Node JS Synchronous database call

a 夏天 提交于 2019-12-02 04:28:56

You can have Sequelize return once the data has actually returned using this method:

return sequelize
  .query("Database query here")
  .success(function(result) {
      //Do something with result here
      else {
          //Return error
      }
  });

Hope the following can help:

(function iterate() {
   executeDatabaseCall(..., function(e,r) {
      if (...conditions to continue looping are verified...)
         iterate();
      else
         ...manage the end of loop...
   });
})();

If you really want to use sync call, you can use this library :

https://github.com/luciotato/waitfor

It'll permit you to make sync call using :

var syncDataReceived = wait.forMethod(collection,'insert',{data:toinsert});

I have solved the problem by move the database call to the loop code like this:

var results = []
async.doWhilst(function(callback) {
    //some code
    sequelize.query('some query').success(function(result) {
        results = result;
        callback();
    });
}, function() {
    //use the results variable that is fetched from the database
    //return true to continue looping or false to stop here
}, function(err) {
   //do some things when the loop finishes
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!