Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pouchdb - get all data from database

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);
});
like image 540
Rajesh K Jeyapaul Avatar asked Mar 14 '23 05:03

Rajesh K Jeyapaul


1 Answers

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.

like image 90
Bradley Holt Avatar answered Apr 27 '23 00:04

Bradley Holt