Contentful API returning 'version mismatch' on entry update

和自甴很熟 提交于 2019-12-13 07:24:32

问题


I'm attempting to do the following with the Content Management API for Contentful:

  1. Get an entry (entry1)
  2. Find another entry (entry2) using data from a field in entry1
  3. Update entry1 with data from entry2

My code looks like this:

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

    });


  });
});

Steps 1 and 2 work fine but the final step, updating the original entry, keeps returning the following error:

Response: {
  "sys": {
    "type": "Error",
    "id": "VersionMismatch"
  },
  "requestId": "content-api:2PSSF6RtpSs2YyaaisK2wc"
}

How do I stop this happening? I've tried everything I can think of including manually updating the sys.version number, but when updating it seems to ignore any sys data I provide.


回答1:


Refer to http://docs.contentfulcma.apiary.io/#introduction/resource-ids and the section that is titled 'Updating and Version Locking'

You need to pass the version as a header parameter called "X-Contentful-Version" with the PUT request.



来源:https://stackoverflow.com/questions/32787795/contentful-api-returning-version-mismatch-on-entry-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!