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) {
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