简体   繁体   English

在Cradle Merge中使用变量(Node.js和CouchDB)

[英]Using variables in Cradle Merge (Node.js and CouchDB)

I'm trying to write a function that loops and changes a field in multiple documents at the same time. 我正在尝试编写一个函数,该函数可以同时循环和更改多个文档中的字段。 The only issue is that when I'm passing a parameter value, the cradle merge seems to actually pass a field with the parameter name instead of the value. 唯一的问题是,当我传递参数值时,通讯录合并似乎实际上传递的是带有参数名称而不是值的字段。

For example: 例如:

function saveToAll(field, data) {
  db.get('document_list', function (err, doc) {
      for (key in doc.doc_list_pure) {
        //Create a Closure
        (function(key1) {
          console.log(key1)
            //Go into the DB
            console.log(field);
            console.log(data);
             db.merge(key1, {
              field : data
            }, function (err, res) {
                console.log('Saved');
            });
         }
        )(key)
      }
});
}

So here, if I write a function like this: 所以在这里,如果我编写这样的函数:

saveToAll("new_field", value);

It will log correctly, but save a field literally called 'field' with the correct data. 它将正确记录日志,但保存带有正确数据的字面意义为“ field”的字段。 Does anyone have an idea of how to approach this? 有谁知道如何解决这个问题? Is this a cradle bug or just me? 这是一个摇篮虫还是我?

Try this: 尝试这个:

function saveToAll(field, data) {
  db.get('document_list', function (err, doc) {
      for (key in doc.doc_list_pure) {
        //Create a Closure
        (function(key1) {
          console.log(key1)
            //Go into the DB
            console.log(field);
            console.log(data);
            var dataObj = {};
            dataObj[field]=data;

            db.merge(key1, dataObj, function (err, res) {
                console.log('Saved');
            });
         }
        )(key)
      }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM