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.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Subburaj
  • 5,114
  • 10
  • 44
  • 87
  • 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. – Paul Mar 28 '14 at 10:27

1 Answers1

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.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 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 Mar 28 '14 at 10:49
  • 1
    @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: http://stackoverflow.com/questions/22539815/arent-promises-just-callbacks explaining the advantages of the approach I suggested. You also get 'converting the `.then` promise to a node callback' for free with `.nodeify` – Benjamin Gruenbaum Mar 28 '14 at 10:52