3

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!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

6

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/customersViewwould 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 :)

Alexis Côté
  • 3,670
  • 2
  • 14
  • 30
  • 2
    Alexis, thank you SO much! I was really confused about the design name. Now it works. I need to find out how to get a doc value, but I'll take a look at documentation. Thank you again! – Queila Martins Nov 04 '16 at 20:52
  • how do I accept your answer? I'm looking for something like a button to do that, but I still didn't find it. Sure I want to do it, you were very nice! – Queila Martins Nov 04 '16 at 22:03
  • To accept the answer, you normally have a check to click on right under the number of votes. Votes are defined to the left of the answer and you can upvote or downvote if you find it useful. – Alexis Côté Nov 05 '16 at 03:56
  • Thanks a lot. For many minutes I was staring at the screen and trying to figure out what was wrong. – Aykut Kllic Oct 11 '21 at 11:03