0

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.

Nie Selam
  • 1,343
  • 2
  • 24
  • 56

1 Answers1

0

Problem :

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}) // <---- ISSUE IS HERE
    amount= amount - 30;
}

You are looping but you are not waiting to update completion , you should wait for update completion and then go to the next updation


Solution :

If you are using async/await , then use it like :

async function FUNCTION_NAME() { // <----- async should be here
    ...
    await db.ModelName.update({balance:newBalance}); // <----- await should be here
    ....
}

OR Promise.all

let promises = []; // <---- HERE
for(let i=0; i<2; i++){
    //update records
    console.log('updating row ' , i);
    const currentBalance= updateRecords[i]['current_balance']
    const newBalance= 30+ currentBalance 
    promises.push(db.ModelName.update({balance:newBalance})) // <---- HERE
    amount= amount - 30;
}

if (amount >0){
    Promise.all(promises).then(data => {
        console.log('remain amount'); // <---- CHECK HERE
    });
}
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
  • using await... causes me error "await is a reserved keyword". – Nie Selam Dec 06 '18 at 07:04
  • In order to use await, the function directly enclosing it needs to be async. , So it means you are not using async/await , for that you have to make function async and then you can use it , please check updated answer , if you dont want to use that , you can go for second option. – Vivek Doshi Dec 06 '18 at 07:12
  • Thanks. The functions are in a class so what i have is class done { async methodname(){ await...} ...} so yes I do have async in that method. Let me try second method. – Nie Selam Dec 06 '18 at 07:23
  • Then it should not throw an error like "await is a reserved keyword" , better way is to use async/wait. https://stackoverflow.com/questions/42299594/await-is-a-reserved-word-error-inside-async-function – Vivek Doshi Dec 06 '18 at 07:29
  • can we take this to the chat room for a minute Vivek if you don't mind? Please. – Nie Selam Dec 06 '18 at 07:52
  • Sure .@NieSelam – Vivek Doshi Dec 06 '18 at 07:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184794/discussion-between-vivek-doshi-and-nie-selam). – Vivek Doshi Dec 06 '18 at 07:56