简体   繁体   中英

How to get documents from CouchDB database in Node.js

I'm developing a simple application using CouchDB and Node.js. I have few documents in my database and created a view to retrieve the documents. How can I get the view from CouchDB in my javascript file?

 app.get('/find', function(req, res){

var cradle = require('cradle');
var connection = new(cradle.Connection)('http://localhost:5984/', 443);
var db = connection.database('db');

db.view('_design/usersView/users', function (err, response) {
        console.log(response);
});

The response says: undefined

Try querying the view like this

db.view('usersView/users', function (err, response) {
    console.log(response);
});

You don't need a _design/ with cradle (although you need that if you query it with default couchdb api)

Also I think it would be better if you moved the connection and the require statements out of the the controller.

I solved the problem. Just sent http get request to couchDB api to retrieve the view with this command:

db.get('_design/usersView/_view/users', function (err, doc) {
   res.end(JSON.stringify(doc));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM