Retrieving all documents from couchdb using Node.js

I am writing a simple test application to experiment with the functionality of node.js and couchdb, so far I love it, but I ran into a trap. I searched extensively, but cannot find the answer. My test server (simple address book) does 2 things:

  • if the user goes to localhost:8000/{id} , then my application returns the name and address of the user with this id.
  • if the user goes to localhost:8000/ , then my application needs to return a list of names that are hyperlinks and transfers them to the localhost:8000/{id} page.

I managed to get the first requirement. I cannot find how to get a list of all the names from my couchdb. this is what i need help with. here is my code:

 var http = require('http'); var cradle = require('cradle'); var conn = new(cradle.Connection)(); var db = conn.database('users'); function getUserByID(id) { var rv = ""; db.get(id, function(err,doc) { rv = doc.name; rv += " lives at " + doc.Address; }); return rv; } function GetAllUsers() { var rv = "" return rv; } var server = http.createServer(function(req,res) { res.writeHead(200, {'Content-Type':'text/plain'}); var rv = "" ; var id = req.url.substr(1); if (id != "") rv = getUserByID(id); else rv = GetAllUsers(); res.end(rv); }); server.listen(8000); console.log("server is runnig"); 

As you can see, I need to populate the GetAllUsers () function. Any help would be greatly appreciated. Thanks in advance.

+4
source share
2 answers

You can create a CouchDB view that lists users. Here are a few resources on CouchDB presentations that you should read to get a more detailed picture on this topic:

So, let's say you have these documents:

 { "_id": generated by CouchDB, "_rev": generated by CouchDB, "type": "user", "name": "Johny Bravo", "isHyperlink": true } 

Then you can create a CouchDB view (part of the map) that looks like this:

 // view map function definition function(doc) { // first check if the doc has type and isHyperlink fields if(doc.type && doc.isHyperlink) { // now check if the type is user and isHyperlink is true (this can also inclided in the statement above) if((doc.type === "user") && (doc.isHyperlink === true)) { // if the above statements are correct then emit name as it key and document as value (you can change what is emitted to whatever you want, this is just for example) emit(doc.name, doc); } } } 

When the view is created, you can request it from the node.js application:

 // query a view db.view('location of your view', function (err, res) { // loop through each row returned by the view res.forEach(function (row) { // print out to console it name and isHyperlink flag console.log(row.name + " - " + row.isHyperlink); }); }); 

This is just an example. First, I would recommend exploring the above resources and exploring the basics of CouchDB views and its capabilities.

+7
source

I expect you to do something like (using nano, which is the library I created):

 var db = require('nano')('http://localhost:5984/my_db') , per_page = 10 , params = {include_docs: true, limit: per_page, descending: true} ; db.list(params, function(error,body,headers) { console.log(body); }); 

I'm not quite sure what you are trying to accomplish using http , but feel free to come to my blog if you are looking for a few more examples. Just wrote a blog post for people starting with node and couch

As mentioned above, the time will come when you will need to create your own view. Check the CouchDB API Wiki , then scan through the book , check that the project documents , then if you like, you can go and check the test code that I have for creating and requesting views .

+7
source

All Articles