I’m using contentful-managament js lib (v10.19.3) to automate content creation.
I have three linked entities – Singer → Albums → Tracks
I create a contentful client:
const space = await contentful.createClient({ accessToken: '...'}).getSpace(space);
const client = await space.getEnvironment('..');
Then create a track an publish it:
const track = await client.createEntry(contentType.track, {
fields: {
title,
link,
}
});
await track.publish();
Create an album with the track link and publish it:
const album = await client.createEntry(contentType.album, {
fields: {
title,
tracks: [{
sys: {
type: 'Link',
linkType: 'Entry',
id: track.id,
}],
}
});
await album.publish();
And then I add an album link to the existing Singer entry:
const singer = await client.getEntry(singerId);
const existingAlbums = singer.fields.albums || [];
const albumLink = {
sys: {
type: 'Link',
linkType: 'Entry'
id: album.id
}
}
const mergedAlbums = [...existingAlbums, albumLink];
singer.fields.albums = mergedAlbums;
await singer.update();
await singer.publish();
And after calling the publish method I’m getting VersionMismatch error:
VersionMismatch: {
"status": 409,
"statusText": "Conflict",
"message": "",
"details": {},
"request": {
"url": "/spaces/.../environments/develop/entries/.../published",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/vnd.contentful.management.v1+json",
"X-Contentful-User-Agent": "sdk contentful-management.js/10.19.3; platform node.js/v18.9.0; os macOS/v18.9.0;",
"Authorization": "Bearer ...",
"user-agent": "node.js/v18.9.0",
"Accept-Encoding": "gzip",
"X-Contentful-Version": 40
},
"method": "put",
"payloadData": null
},
"requestId": "..."
}
If I remove publish method, everything goes fine, but the singer entry stays in changed
status and I have to publish it manually.
As I understood from the docs, the reason is the wrong X-Contentful-Version
header value, but I don’t set it. And even I didn’t find the way to set it with the contentful-management
lib.
How do I fix it? Should I somehow set the version number explicitly?