Javascript 内容API返回';版本不匹配';输入时更新
我正在尝试使用Contentful的内容管理API执行以下操作:Javascript 内容API返回';版本不匹配';输入时更新,javascript,json,node.js,api,contentful,Javascript,Json,Node.js,Api,Contentful,我正在尝试使用Contentful的内容管理API执行以下操作: 获取条目(entry1) 使用entry1中字段的数据查找另一个条目(entry2) 使用entry2中的数据更新entry1 我的代码如下所示: client.getSpace("xxxxxxxx").then(function(space){ space.getEntries({ "content_type": "xxxxxxxx", "sys.id": "2KEZYJOgDSeQMCQIE0Oo88",
client.getSpace("xxxxxxxx").then(function(space){
space.getEntries({
"content_type": "xxxxxxxx",
"sys.id": "2KEZYJOgDSeQMCQIE0Oo88",
"limit": 1
}).then(function(places){
//search for relevant category entry
space.getEntries({
"content_type": contentType.category,
"sys.id": places[0].fields.category["en-GB"],
"limit": 1
}).then(function(category){
//update place object
places[0].fields.categoryNew = {
"en-GB": [
{ sys: { type: "Link", linkType: "Entry", id: category[0].sys.id } }
]
};
//update place
request({
method: 'PUT',
url: 'https://api.contentful.com/spaces/xxxxxxxx/entries/' + places[0].sys.id,
headers: {
'Authorization': 'Bearer xxxxxxxx',
'Content-Type': 'application/vnd.contentful.management.v1+json',
'X-Contentful-Content-Type': 'xxxxxxxx'
},
body: JSON.stringify({fields:places[0].fields})
}, function (error, response, body) {
console.log(body);
});
});
});
});
第1步和第2步工作正常,但最后一步,即更新原始条目,会不断返回以下错误:
Response: {
"sys": {
"type": "Error",
"id": "VersionMismatch"
},
"requestId": "content-api:2PSSF6RtpSs2YyaaisK2wc"
}
我如何阻止这种情况发生?我已经尝试了我能想到的一切,包括手动更新sys.version
编号,但在更新时,它似乎忽略了我提供的任何sys
数据。请参阅和标题为“更新和版本锁定”的部分。
您需要在PUT请求中将版本作为名为“X-Contentful-version”的头参数传递