I'm new at node.js and couchdb, so I've been studying and performing some code tests to try to understand how they work.
I'm using nano package to work with couchDBs in node.js. I was able to connect to a database and also to insert data into it. But I've been struggling to understand how to query data.
I read a documentation about views in couchDB and since then I've been trying to retrieve data from a view a created, called customersView (database name is testsqueila).
View's design name is "_design/custdoc/_view/customersView". The function I'm trying to make work is below:
testsqueila.view('_design/custdoc/_view/customersView', 'customersView', function(err, body) {
if (!err) {
body.rows.forEach(function(doc) {
console.log(doc.value);
});
}
});
There is something wrong happening, as nothing is printed in console. I'm not sure if I'm using the view's design name in a wrong way or something else. Does anyone have an idea of what's going on? I also added some code to get err value, and in the end of the message in console it says: errid: 'non_200' description: 'couch db returned 404'.
Thank you for any help!
First, I suggest you take a look at the nano documentation which details everything you need to know about the API calls.
The _design/custdoc/_view/customersView
would be only valid if you were querying directly the HTTP API of CouchDB. With nanodb, you need to do the following :
testsqueila.view('custdoc', 'customersView', function(err, body) {
if (!err) {
body.rows.forEach(function(doc) {
console.log(doc.value);
});
}
});
So the .view() signature looks like this : db.view(designname, viewname, [params], [callback])
Designname is the design document id without the _design/ prefix. In your case, it's custdoc.
Viewname is the name of the view that you want to query. In your case, it's customersView.
Feel free to comment if some points are unclear! Welcome to the CouchDB community :)