1

In my node application i have to execute 2 queries 1 after the other (i.e) I have to execute 2nd query based on result of 1st query.

My code:

 var levels;
        try {
            sequelize.query("Select * from levels where country_id = " + level0 + "").success(function(results) {
                levels = results;               
            }).failure(function(err) {
                if (err) {
                    logger.error(err.stack);
                    throw (err);
                }
                else {
                    if (callback) {
                        callback(err, null);
                    }
                }
            });
        }
        catch (err) {
        }


if (level == 2) {
        query = "select *from xxxxxx where " + query + " and leveltype = "+levels[0].name_2+"";-----------------------------------------------------------> this is from 1st query

        try {
            sequelize.query(query).success(function(results) {
                results = tsv.stringify(results);
                onSuccess(results, callback)
            }).failure(function(err) {
                if (err) {
                    logger.error(err.stack);
                    throw (err);
                }
                else {
                    if (callback) {
                        callback(err, null);
                    }
                }
            });
        }
        catch (err) {
            logger.error(err.stack)
        }
    }

Please help me to solve this. Thanks in advance.

1
  • I am unfamiliar with the sequelize module. In node_sqlite3 one would sequence queries by sending the 2nd query in the success callback of the 1st query, and then in the success callback of the 2nd query you know both have succeed. Commented Mar 28, 2014 at 10:27

1 Answer 1

3

Sequelize uses Bluebird promises:

sequelize.query("Select * from levels where country_id = " + level0 + "").success(function(results) {
                levels = results;               
            }).failure(function(err) {
                if (err) {
                    logger.error(err.stack);
                    throw (err);
                }
                else {
                    if (callback) {
                        callback(err, null);
                    }
                }
            });
        }

Can be turned into:

sequelize.query("Select * from levels where country_id = " + level0 + "")
         .then(function(results)  {
            levels = results;               
        });

Promises chain:

sequelize.query("Select * from levels where country_id = " + level0 + "")
.then(function(levels)  {
     var query = "select *from xxxxxx where " + query + 
                 " and leveltype = "+levels[0].name_2+""
     return sequelize.query(query);
 }).nodeify(callback); // turn back from promise API to callback API

And this code basically does everything your above code does :)

  • You don't need try/catch and if(err) checks with promises - they take care of it for you. If you throw inside a promise handler you're safe.
  • Promises chain, if you return the value of a promise from a .then it'll resolve it with that promise (meaning you can add a .then handler)

You can do better though, you can return the value of the promise (that is - return sequelize.query) and switch to a promise API entirely.

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

2 Comments

Thanks for your valuabletime.. As @Paul sad i have executed 2nd query in the success module of first..Its working for me..Whether its advised do that.. Also for your above code whether i have to install any package??
@Subburaj which code do you find longer and more readable? Also, what Paul suggested is missing the point of the powerful promise API you get for free - see my Q&A here: stackoverflow.com/questions/22539815/… explaining the advantages of the approach I suggested. You also get 'converting the .then promise to a node callback' for free with .nodeify

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.