如何使用nodejs cloudant模块更新cloudant中的数据?

时间:2015-01-31 10:19:42

标签: json node.js couchdb cloudant

下面提到的示例json文档。它包含两个字段。

{
  "_id": "daef4a0e39c0c7a00feb721f6c4ce8b9",

  "_rev": "2-8c7ef28df59ecbdaa23b536e58691416",

  "name": "sukil",

  "skills": "java"
}


In server.js

var express = require('express');

var app = express();

var cloudant = require('cloudant');

cloudant({account:"test", password:"test"}, function(err, cloudant) {

var alice = cloudant.use('opti-update')
alice.atomic("_design/sample", "inplace", "daef4a0e39c0c7a00feb721f6c4ce8b9", {field: "name", value: "bar"}, function (error, response) {
 console.log(error+""+response);
})
})

这里_design / sample是一个设计文档名称,inplace是更新函数名,然后是文档id.It返回错误是文档更新冲突和响应未定义。

In design document mentioned below

{
  "_id": "_design/sample",

  "_rev": "9-94393ee4665bdfd6fb283e3419a53f24",

  "updates": {

    "inplace": "function(doc,req){var field = req.body.field;var value = req.body.value;doc[field] = value;return [doc,''];}"

  }
}


I want to update the data in cloudant using node cloudant module. I want to update the name field in json document.Above method i tried but it shows document update conflict error.How to resolve this?

1 个答案:

答案 0 :(得分:0)

原子方法假定第一个参数仅作为设计文档。因此需要明确指定"_design"

alice.atomic("sample", "inplace", "daef4a0e39c0c7a00feb721f6c4ce8b9", {field: "name", value: "bar"}, function (error, response) {
 console.log(error+""+response);
})

这可能导致问题。