首页 > 解决方案 > 如何使用 node.js 获取 pouchdb 文档的所有 id

问题描述

我想知道如何使用 node.js 从 pouchdb 获取某个数据库的所有 id 这是我的尝试,但你可以猜到它没有工作,特别是我需要将所有 id 放入一个数组数字,只有 id 没有别的。我是 javascript 和数据库的初学者

var PouchDB = require('PouchDB');
var db = new PouchDB('mes_iab_db');

db.allDocs({
   include_docs: true,
   attachments: true
 }).then(function (result) {
   console.log(result)
   var new1 = JSON.stringify(result.rows);
   console.log(new1);
   new1.filter(id=>{id=_id;})
 }).catch(function (err) {
   console.log(err);
 });

标签: javascriptnode.jsdatabasepouchdb

解决方案


尝试这样的事情

db.allDocs({
  include_docs: true,
  attachments: true,
})
  .then(function (result) {
    if (result.rows && result.rows.length) {
      return result.rows.map(({ doc }) => doc._id);
    }
    return [];
  })
  .then(console.log)
  .catch(function (err) {
    console.log(err);
  });

推荐阅读