Subburaj
Subburaj

Reputation: 5192

node.js + sequelize + query execution

In my node app i am using sequelize ORM. In that i have to execute 3 queries and after all are exxecuted i have to combine those results into JSON format.

My queries:

try {
    sequelize.query("select id_0, name_0, id_1, name_1 from xxxx group by id_0, name_0, id_1, name_1").success(function (result) {
        finalResult = result;

    })
} catch (err) {

}

try {
    sequelize.query("select yyyyyyyyyy(JSON datatype) as value from xxxxx limit 1").success(function (valueResults) {
        valueResults = valueResults[0].value;
        valueResults = JSON.parse(valueResults);
        for (var prop in valueResults) {
            keyResult.push(prop);
        }
    })
} catch (err) {

}

try {
    sequelize.query("select country_name, level0, level1, level2, level3, level4 from levels").success(function (result) {
        //console.log("ccccccc=" +util.inspect(levelsResult)); = result;
        levelsResult = result;

    })
} catch (err) {

}

I have to combine the 3 outputs into single and i have to format into JSON. when i tried to print these 3 outputs at last its coming as empty because of async calls.Please help me to solve this.Thanks in advance.

Upvotes: 0

Views: 2291

Answers (2)

clay
clay

Reputation: 6017

You can use a flow control package to help with this. There are pure JS ways of doing so, but a package like async or some promise implementation would work just as well (unless you want the exercise).

An example in async would be the async.series or async.parallel method (unsure your call sequence if they need to be sequential or parallel). Like this:

var async = require('async');

async.parallel({
  query1 : function( cb ){
    //perform query here. Put cb() inside callback, where you get results
    cb(null, "result1");
  },
  query2 : function( cb ){
    //perform query here. Put cb() inside callback, where you get results
    cb(null, "result2");
  },
  query3 : function( cb ){
    //perform query here. Put cb() inside callback, where you get results
    cb(null, "result3");
  }
},function parallelFinal(parallelErr, parallelResults){
  if( parallelErr ) throw new Error("Something bad!");
  console.log("Results are " + JSON.stringify( parallelResults ) );
});

Results in:

Results are {"query1":"result1","query2":"result2","query3":"result3"}

You can set and manipulate the objects in the resulting method as needed.

Upvotes: 2

Guillermo Mansilla
Guillermo Mansilla

Reputation: 3889

Either go with the solution @clay told you or do the queries inside the query callback, that is to say:

var result1, result2, result3
try {
    sequelize.query("select foo from bar").success(function (foo) {
    result1 = foo;
    try {
        sequelize.query("select foo2 from bar2").success(function (bar) {
            result2 = bar;
            //do the 3rd query here
        });
    } catch (e) {

    }
})
} catch (err) {

}

Bear in mind that this approach is not practical when you want to do several queries, this approach is a simple blocking code, that is the opposite to how node.js works (non-blocking)

Upvotes: 1

Related Questions