I have a use case where a dataset has to be iterated and loaded into the database. I am using an async function for the purpose,
const dataset = []
dataset.forEach(async(data) => {
const filter = {id:data.id}
const count = await datamodel.find(filter).count();
if(count===0){
/*Insert Data*/
datamodel.save(data)}
else{
/* Update Data*/
datamodel.upsert(data)}
})
But the database inserts are not as expected. Only the last entry of the dataset gets updated multiple times which is due to async and await in code.
How can I rewrite the code to perform the desired operation in a sequential fashion?
Note: .then() also has the same output.