Hi, I’m trying to resolve an issue where I have a couple of functions (findOne, updateOne) chained within a for next loop.
When executed it updates the wrong item - (probably because the for-next loop has advanced to the next item)
However, when I use only ‘updateOne’ alone (removed findOne), it updated the correct item. I’m unsure if it works because the update completes before the for-next is advanced or there is some error in the .then chain due to async behaviour. I’ve tried adding await before updateOne & findOne, but this errors ‘await is only valid in async function’.
Here’s a snippet of code that updates the wrong item, any assistance appreciated.
setInterval(function () {
dbProduct.find({
bidEnded: false,
productStatus: 'Live'
}).then(dbProductRes => {
for (var i = 0; i < dbProductRes.length; i++) {
var product = dbProductRes[i].toObject();
dbBidder.findOne({
bidderToken: winningBidder
}).then(res => {
dbProduct.updateOne({
productId: product.productId
}, {
$set: {
productStatus: "Sold-PaymentSuccessful"
}
}).exec();
});
}
});
}, 15000);