Contentful API 在条目更新时返回“版本不匹配”

Coo*_*oop 4 javascript api json node.js contentful

我尝试使用 Contentful 的内容管理 API 执行以下操作:

  1. 获取条目(条目1)
  2. 使用条目 1 中字段的数据查找另一个条目 (条目 2)
  3. 使用条目 2 中的数据更新条目 1

我的代码如下所示:

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);
      });

    });


  });
});
Run Code Online (Sandbox Code Playgroud)

步骤 1 和 2 工作正常,但最后一步(更新原始条目)不断返回以下错误:

Response: {
  "sys": {
    "type": "Error",
    "id": "VersionMismatch"
  },
  "requestId": "content-api:2PSSF6RtpSs2YyaaisK2wc"
}
Run Code Online (Sandbox Code Playgroud)

我该如何阻止这种情况发生?我已经尝试了我能想到的一切,包括手动更新号码sys.version,但更新时它似乎忽略了sys我提供的任何数据。

小智 5

请参阅https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/updating-and-version-locking

您需要将版本作为名为“X-Contentful-Version”的标头参数与 PUT 请求一起传递。