最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Contentful API returning 'version mismatch' on entry update - Stack Overflow

programmeradmin4浏览0评论

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: '/' + 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.

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./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.

Share Improve this question edited Dec 26, 2024 at 7:58 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Sep 25, 2015 at 17:41 CaribouCodeCaribouCode 14.4k33 gold badges111 silver badges186 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Refer to https://www.contentful./developers/docs/references/content-management-api/#/introduction/updating-and-version-locking

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

As mentioned in the accepted answer, you are missing the X-Contentful-Version header but that's possibly not the cause of the problem.

Contentful wants the "current version" (as per docs), which is not necessarily the same as the published version. It wants to check that you are not updating an old version of the content. Both the latest (i.e. "current") version and the published version are present in the response to GET for an entry. Make sure you use the correct one in the X-Contentful-Version header or you will get this error. If you use one of the official SDKs, it should handle this for you automatically. But if you are using e.g. Golang or curl, you'll have to do it yourself as above.

It may also be a PATCH request that you need here rather than PUT but that's a different matter.

发布评论

评论列表(0)

  1. 暂无评论