PouchDB - 从数据库获取所有数据

4

我需要从PouchDB数据库获取所有文档。有人能告诉我如何从回调函数中获取'doc'吗?我想将它作为响应对象返回,并在控制台输出中获取它。

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);
});
1个回答

7
我认为你想要的是以下内容:
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);
});

如果出现错误,它将由您的catch回调处理,因此您的then回调中只有一个参数(result)。 result变量将包含关于结果的一些元数据(例如total_rows),以及一个rows属性,它将是数据库中所有文档的数组。


谢谢,我通过使用“db.get(response.rows[i].id, function (err,doc){JSON.stringify(doc.title).....”使其工作。 - Rajesh K Jeyapaul
1
@RajeshKJeyapaul 这样做是可行的,但会导致额外且不必要的数据库查询。在我的示例中,result.rows 属性应包含您需要的所有数据。例如,result.rows[i].doc。如果您想遍历行:result.rows.map(function(row) { console.log(row.doc); }); - Bradley Holt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接