I have a problème which I can't seem to figure out. I need this await call to happen only when it finishes one by one in the loop, but it seems like its just calling it twice in the same time quickly, which is not making the api post work correctly.
I've tried to use async, await, but the await doesn't seem to like it being in two loops. Has anyone got a better solution?
async pushToCreate() {
const toCreate = this.json.toCreate
let counter = 0
// toCreate Sample = { en: [{}, {}] }, { fr: [{}, {}] }
Object.keys(toCreate).forEach((item) => {
toCreate[item].forEach((el) => {
const lang = item
const dataContent = el
await this.wp.products().param('lang', lang).create({
title: dataContent.dgn,
fields: dataContent,
status: 'publish'
}).then(function( response ) {
counter++
}).catch(function(err) {
console.log('error in create', err)
})
console.log('await finished')
})
})
// should console log when above is finished
console.log('END')
}