Batch updating a content type. best practice?

Is there a simple code sample for batch updating data. I get Rate limit. What is best practice when reading from one system and updating contentful?

Something like this

Myarray = [ all records in my database]
for (var i = 0; i < Myarray.lenght; i++) {

If (Myarray[i].contentfulID ===null) {
createContentfulEntry( Myarray[i] ) // is not in contentful. create the record
} else
updateContentfulEntry( Myarray[i] , Myarray[i].contentfulID) // is in contentful. update the record

}

function updateContentfulEntry(myRecord, contentfulId) {

contentfulUpdateClient.getSpace(globalSpaceId)
    .then((space) => space.getEnvironment('master'))
    .then((environment) => environment.getEntry(contentfulId)) //read the full record

    .then((entry) => {
        console.log('Update :' + myRecord.title)
	entry.fields.displayName['en-US'] = myRecord.title

    })
    .then((entry) => {
        console.log(`Entry Update OK.`)
    })
    .catch((err) => {
        console.log(`Update  ERROR.`+ JSON.stringify(err))
    })

}

Hi @terje,

Could you confirm what is the version of your SDK and node itself that is being used for this?

Node version 8.11.1 and latest SDK

But I found a solution to the problem. Defined my function as async
async function updateContentfulEntry(…

And then put await on
await contentfulUpdateClient.getSpace(globalSpaceId)

So now I do not have the rate problem :slight_smile:

Strange behaviour when updating an entry.

I found that if a field defined on a content type is empty. Then the field is not part of the record you get when you read using getEntry. The result is that you can’t write to it.

See the example code below

Say that the content type contains a field named “sirName”

For the first 10 entries the field has a value. Eg entry.fields.sirName[‘en-US’] is Olsen and so on.
But if entry number 11 does not have a value. Then entry.fields.sirName[‘en-US’] is simply not in entry.fields. And if you set it to a value eg entry.fields.sirName[‘en-US’] = “Obama”
And then try to write using entry.update() then you will get the error

“Cannot set property ‘en-US’ of undefined”
"TypeError: Cannot set property ‘en-US’ of undefined”

Is there example code for batch updating somewhere? It would save everyone a lot of time if there was a simple best practice code example.

Code below:

Myarray = [ all records in my database]
for (var i = 0; i < Myarray.lenght; i++) {

If (Myarray[i].contentfulID ===null) {
createContentfulEntry( Myarray[i] ) // create it
} else
updateContentfulEntry( Myarray[i] , Myarray[i].contentfulID) //update it

}

async function updateContentfulEntry(myRecord, contentfulId) {

await contentfulUpdateClient.getSpace(globalSpaceId)
.then((space) => space.getEnvironment(‘master’))
.then((environment) => environment.getEntry(contentfulId)) //read the full record

    .then((entry) => {
        console.log('Update :' + myRecord.title)
	entry.fields.displayName['en-US'] = myRecord.title

entry.fields.sirName[‘en-US’] = myRecord.lastName
return entry.update()
})
.then((entry) => {
console.log(Entry Update OK.)
})
.catch((err) => {
console.log(Update ERROR.+ JSON.stringify(err))
})
}

1 Like