0

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.

1
  • Please don't continually edit your question. If you want to bring more attention to it, add a bounty. Commented Apr 7, 2014 at 18:58

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.