【问题标题】:How do I read and save all cloudant documents in a list如何读取并保存列表中的所有 cloudant 文档
【发布时间】:2020-05-13 17:13:06
【问题描述】:

因此,我尝试使用 Node.js 从 cloudant 数据库中提取所有数据,但在弄清楚如何获取所有文档时遇到了一些问题。基本上,我建立了一个完整的反应网页,但我是 Node.js 和 cloudant 的新手。仅使用节点运行命令,我可以连接到我的数据库,拉取/记录数据库信息,但是我无法访问文档,然后将其所有数据放入列表中,因此我可以创建数据的表格视图。任何建议都会令人惊叹,我显然出于安全目的删除了我的凭据。


var cloudant = new Cloudant({ url: 'xxxx', plugins: { iamauth: { iamApiKey: 'xxxx' } } });

cloudant.db.list(function(err, body) {
    body.forEach(function(db) {
     console.log(db);

     });

});`


var readDocument = function(callback) {
    console.log("Reading document 'slay-data'");
    cloudant.db.get('slay-data', function(err, data) {
      console.log('Error:', err);
      console.log('Data:', data);
      // keep a copy of the doc so you know its revision token
      doc = [data];


      callback(err, data);
      //console.log(doc);
    });
    cloudant.db.list(function (err, data) {
        console.log(err, data);
      });
};
var docu = readDocument();
console.log(docu)

【问题讨论】:

  • 您能否描述一下预期的输出以及您遇到的情况?

标签: node.js nosql cloudant


【解决方案1】:

如果您想从 Cloudant 数据库中获取所有数据,您可以这样做(我已切换到 async/await 节点,因为它更易于阅读:

const cloudant = new Cloudant({ url: 'xxxx', plugins: { iamauth: { iamApiKey: 'xxxx' } } });
const db = cloudant.db.use('mydb')

const readAllData = async function () {
  // extract all the data, including the document bodies
  const response = await db.list({ include_docs: true })
  // response.rows is an array with a 'doc' attribute for each document
  const docs = response.rows.map((r) => { return r.doc })
  return docs
}

readAllData()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多