【问题标题】:function callback not working with cloudant.db函数回调不适用于 cloudant.db
【发布时间】:2021-07-14 22:36:15
【问题描述】:

我正在尝试在 cloudant NoSQL 数据库中插入一个文档,但是当我执行此函数时,它会一直运行到“console.log(db)”,但不执行 createDocument 函数。我想不通为什么。谢谢!

function addCard(id, title, texto){
    
    var cloudant = new Cloudant({ url: '...', plugins: { iamauth: { iamApiKey: '...' } } });
    
    cloudant.db.list(function(err, body) {
body.forEach(function(db) {
 console.log(db);
 var createDocument = function(callback) {
  console.log("Creating document 'mydoc'");
  // specify the id of the document so you can update and delete it later
  db.insert({ _id: 'mydoc', a: 1, b: 'two' }, function(err, data) {
    console.log('Error:', err);
    console.log('Data:', data);
    callback(err, data);
  });
};
 
 });
});
    

    
    console.log(id, title, texto);
}

【问题讨论】:

    标签: nosql cloudant


    【解决方案1】:

    我想补充一下我是如何解决的。在我的情况下创建函数是不必要的。一位朋友帮助了我,这就是现在(和工作)的方式。

      const cloudant = new Cloudant({
        url: "...",
        plugins: { iamauth: { iamApiKey: "..." } },
      });
      cloudant.use("dbName").insert({ *doc content* });
      console.log("Datos subidos con éxito");
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多