Fields created via contentful-migration don't exist in getEntry response

Hey Team,

We are currently in the middle of performing our first migration and have updated one of our models with some new fields via the contentful-migration library.

The migration of the new fields is done like so:

const gameInfo = migration.editContentType(‘gameInfo’);
gameInfo
.createField(‘imgUrlPattern’)
.name(‘imgUrlPattern’)
.type(‘Symbol’);
gameInfo.changeFieldControl(‘imgUrlPattern’, ‘builtin’, ‘singleLine’, {
helpText: ‘todo’
});

This works fine and we see the new fields appear in the web app.
However, when trying to update entries and put some values into these new fields via the contentful-management library (following the docs here https://www.contentful.com/developers/docs/references/content-management-api/#/reference/entries/entry/create-update-an-entry/console/js), we found that they were not being returned in the getEntry response:

await environment
.getEntry(entryID)
.then((entry) => {
entry.fields.imgUrlPattern[‘en-GB’] = ‘/test.jpg’;
return entry.update();
});

The problem here is that entry.fields only contains existing fields, and none of the new ones created during the migration are present. This code updates existing fields just fine and also works on the new fields if we manually put a value in there via the web app first.

I guess the question I’m asking is:
Is it expected behaviour for fields that have been created, but never had a value put into them (manually or programmatically at creation) to not be returned by get queries from the contentful-management library?

Cheers

Solved by changing the code thats set the fields value to:

entry.fields[‘imgUrlPattern’] = { ‘en-GB’: '/test.jpg’ };

Still curious as to why brand new fields are not returned in the get queries :slight_smile:

It’s a quirk of Contentful’s API have a default value of undefined (which distinct from null), meaning they don’t appear in the API at all. That’s why it’s important to always check the content type for all possible fields that do exist.

The distinction between undefined and null becomes important when it concerns the fallback chain for locales. undefined will fallback to the next locale in the chain while null terminates it.

I hope this clears it up. :slight_smile:

1 Like