4

I need to get all the documents from PouchDB database . Can anyone tell me how to get the 'doc' from the callback function? I would like to return it as response object and also to get it in console output.

var db = new pouchdb('meetups');
db.allDocs({
    include_docs: true,
    attachments: true
}).then(function (err,res) {
    console.log("Result..."+res);
    res.json({'users':res});
}).catch(function (err) {
    console.log(err);
});
Max90
  • 193
  • 2
  • 14

1 Answers1

7

I think what you want is the following:

var db = new pouchdb("meetups");
db.allDocs({
  include_docs: true,
  attachments: true
}).then(function (result) {
  console.log(result);
  res.json({"users": result.rows});
}).catch(function (err) {
  console.log(err);
});

If there is an error it will be handled by your catch callback, so you only have one parameter (result) in your then callback. The result variable will include some metadata about the results (e.g. total_rows) as well as a rows property which will be an array of all of the documents in your database.

Bradley Holt
  • 618
  • 5
  • 7
  • Thanks..I got it working by using " db.get(response.rows[i].id, function (err,doc){JSON.stringify(doc.title)....." – Rajesh K Jeyapaul Nov 11 '15 at 12:42
  • 1
    @RajeshKJeyapaul That works, but will result in an extra (and unnecessary) query against the database. In my example the `result.rows` property should contain all of the data you need. For example, `result.rows[i].doc`. If you want to iterate through the rows: `result.rows.map(function(row) { console.log(row.doc); });` – Bradley Holt Nov 11 '15 at 13:19