oriuken
oriuken

Reputation: 732

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

Upvotes: 2

Views: 2151

Answers (2)

oriuken
oriuken

Reputation: 732

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.

Upvotes: 2

sajan
sajan

Reputation: 1370

@oriuken, maybe you should try something like this below.

.then(parameters => {   
  return new Promise(async (resolve, reject) => {
    try {
      await myModelObject.update({parameters}, { where: { id: myObjectId }});
      console.log("Sequelize update resolved !")
      return resolve('Promise resolved')
    }catch(e) {
      return reject(e.message);
    }
  });
})

Upvotes: -1

Related Questions