Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize operation hanging when returning the result but working when no returning it

I've been working with Node for about 3 years, but what is happening to me right now is driving me crazy.

My project uses Sequelize for the last 6 months and it's all working perfectly.

If I try to update the myModelObject instance with the following code, it works as expected.(I've change the real code in order to show the behavior in a more cleaner way)

...
.then(parameters => {                                    
   myModelObject.update({parameters}, { where: { id: myObjectId }})
   .then(() => {
      console.log("Sequelize update resolved !")      
   })
})
.then( () => {
   console.log("Promise resolved !")
})

This will make my myModelObject updated in the DB and the console will display:

Promise resolved !

1 second later...

Sequelize update resolved !

But, the strange behavior happens when I add a return before the update of myModelObject:

...
.then(parameters => {                                    
   return myModelObject.update({parameters}, { where: { id: myObjectId }})
   .then(() => {
      console.log("Sequelize update resolved !")
   })
})
.then( () => {
   console.log("Promise resolved !")
})

This makes my code to hung.. the update is never done and a timeout happens. What I mean is that the Sequelize update is never resolved if I want to return it ! All my other Sequelize code is working perfectly !

Any hep will be very appreciate it :)

Node: 12.16.0 || Sequelize: 5.21.6 || pg: 7.18.2

like image 551
oriuken Avatar asked Oct 11 '25 08:10

oriuken


1 Answers

In case anyone is interested, I found the problem !.

Before this method is called, in another part of the code, a Sequelize Transaction was being used to update this same object and the transaction was not yet committed.

Apparently this causes my update to hang.

To solve it, I also passed this transaction to my Update call. Another way to fix it would be to end the transaction before calling my update.

like image 178
oriuken Avatar answered Oct 14 '25 10:10

oriuken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!