I am updating a specific model from a loop. It basically distributes a number into several records until the number is out. Sometimes though, the number could be too much so there will be an amount left at the end of the loop. Now, when that happens, I want put that amount that remained in one of the tables that was updated in the loop.
I am getting the exact record to update but its amount reflects the amount that was there before it was updated in the loop, but in the database it is correctly updated.
My console.log() shows that a correct order did take place when it was run. Each order was first executed in the loop then it went out.
Below is just a sample
amount = 900
ModelName=[{'current_balance':100,'id':1},{'current_balance':90,id:2}]; // Schema structure
for(let i=0; i<2; i++){
//update records
console.log('updating row ' , i);
const currentBalance= updateRecords[i]['current_balance']
const newBalance= 30+ currentBalance
db.ModelName.update({balance:newBalance})
amount= amount - 30;
}
// do we have amount left?
if (amount >0){
console.log('remain amount')
// with Sequelizer SELECT, I am getting the latest record sucesssfully. After adding 30 to it in the looop, here its balance should have been 120. But I still get 90
}
console output is
updating row 0
updating row 1
remain amount
and the database contains correct info after loop is done but still I see the original value before the loop when I check at the end of the loop.