aggaton
aggaton

Reputation: 3350

How to fix VersionMismatch when attempting an update using contenful-management

I am attempting to loop through all published documents in order to generate slugs, however I am getting a VersionMisMatch error on the update. I am assuming it is due to the X-Contentful-Version: 0 in there, how do I know what version to put in there and how do do it, I tried adding headers: {"X-Contentful-Version": 1}, in the createClient, however that did not do anything? Or is something else wrong?

EDIT: if I change the code to use getEntries() instead, I get InvalidEntry error (422), however I see X-Contentful-Version is set to 3 in the header. Is this because getPublishedEntries is deprecated?

{
  "name": "VersionMismatch",
  "message": "{\n  \"status\": 409,\n  \"statusText\": \"Conflict\",\n  \"message\": \"\",\n  \"details\": {},\n  \"request\": {\n    \"url\": \"/spaces/foobar/environments/dev/entries/foobar\",\n    \"headers\": {\n      \"Accept\": \"application/json, text/plain, */*\",\n      \"Content-Type\": \"application/vnd.contentful.management.v1+json\",\n      \"X-Contentful-User-Agent\": \"sdk contentful-management.js/0.0.0-determined-by-semantic-release; platform node.js/v19.6.1; os macOS/v19.6.1;\",\n      \"X-Contentful-Version\": 0,\n
...     
const slugify = require("slugify");
const contentfulMgr = require("contentful-management");
const CONTENTFUL_SPACE = "foobar";

const contentfulMgrClient = contentfulMgr.createClient({
  accessToken: process.env.CONTENT_MANAGEMENT_API_KEY,
  headers: {"X-Contentful-Version": 1},
});
(async () => {
  try {
    const space = await contentfulMgrClient.getSpace(CONTENTFUL_SPACE);
    const environment = await space.getEnvironment(process.env.CONTENTFUL_ENV);
    let skip = 0;
    let total = 10000000000;
    do {
      const entries = await environment.getPublishedEntries({skip: skip, limit: 100});
      skip = entries.skip+100;
      total = entries.total;
      for (const entry of entries.items) {
        if (entry.sys.contentType.sys.id === "foobar") {
          entry.fields.slug = { "en-US": slugify(entry.fields.title["en-US"], {
            lower: true,
            trim: true,
            strict: true,
          })};
          await entry.update();
          await entry.publish();
        }
      }
    } while (skip < total);
    console.debug("Finished Migrating.");
  } catch (ex) {
    console.debug("Error Migrating. " + JSON.stringify(ex, null, 2));
  }
})();

Upvotes: 1

Views: 399

Answers (2)

Tobi
Tobi

Reputation: 21

You're getting the error because you're updating the entry and then trying to publish the original version.

To solve this, first of all you need to make the entry a variable instead of a const, i.e. let entry instead of const entry. Then you can update the variable on each iteration of your for loop like this:

entry = await entry.update();
entry = await entry.publish();

Have been breaking my head over this issue for quite some time too, so hope this fixes it for you too.

Upvotes: 1

pataruco
pataruco

Reputation: 11

The error happens because you hardcoded the header "X-Contentful-Version" to 1.

You are probably misled, as I was, into thinking that this is the version of the Contentful Management API or client, but it refers to the version of the asset/entry that you are manipulating.

You can find the version in the asset/entry response <asset|entry>.sys.version

You can find more info here: https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/updating-and-version-locking

I hope it helps you

Upvotes: 0

Related Questions