Even though the updating is happening successfully in the DB (collection()....
) - the batchIdsAdded
data that's supposed to be pushed into packIdsOwned
and packsOwnedMetadata
arrays in the for
loop is displaying as null
in the response.
I know there's an asynchronous problem in my code but I'm not sure how to tackle it in my code even after taking a look at this popular SO post revolving around this and trying out some of the suggestions there. Unfortunately, I've hit a wall and I'm very stuck.
How can I make it so that all the batchIdsAdded
data is pushed to the arrays correctly?
let packsOwned = result.packsOwned;
let packIdsOwned = [];
let packsOwnedMetadata = [];
let batchIdsAdded = {};
result.packsOwned.push(...batchPackIdInput.map(item => (batchIdsAdded = {"PackId" : item}, collection.updateOne(query, {$push: {"packsOwned" : { $each: [batchIdsAdded]}}}, result['userName']))));
// Data to be displayed in the response
for(let i = 0; i < packsOwned.length; i++) {
packIdsOwned.push(packsOwned[i].PackId);
packsOwnedMetadata.push({
PackID : packsOwned[i].PackId
});
}