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

时间:2015-09-25 17:41:01

标签: javascript json node.js api contentful

我正在尝试使用针对Contentful的Content Management API执行以下操作:

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

    我的代码如下所示:

    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数据。

1 个答案:

答案 0 :(得分:-1)

请参阅http://docs.contentfulcma.apiary.io/#introduction/resource-ids以及标题为“更新和版本锁定”

的部分

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