0

I'm writing a NodeJS v0.10 application with MariaSQL.

i want to create a function that returns the id of a row, and if the row doesn't exist, to create it and then return the id.

this is what I have so far:

TuxDb.prototype.createIfNEDrinkCompany = function(drinkCompany) {
 this.client.query("insert into drink_company(drink_company_name) values(:drink_company) on duplicate key update drink_company_id=drink_company_id",
    {'drink_company' : drinkCompany})
    .on('result',function(res) {
    res.on('end',function(info){
        if (info.insertId > 0) {
                return info.insertId;
        } else {
            this.client.query("select drink_company_id from drink_company where drink_company_name = :drink_company",{'drink_company' : drinkCompany})
                .on('result',function(res){
                    res.on('row',function(row){
                        return row.drink_company_id;
                    });
                });
        }
    });
    });

}

now the problem is that since it's asynchronous, the function ends before the value is returned.

how can I resolve this issue ?

ufk
  • 30,912
  • 70
  • 235
  • 386
  • you've added a new tag called `mariasql` would you mind telling me what it is? [googling](https://www.google.co.in/search?q=MariaSQL&oq=MariaSQL&aqs=chrome..69i57j69i60&sourceid=chrome&es_sm=93&ie=UTF-8&qscrl=1) didn't turn up much. – gideon Jan 09 '15 at 06:24
  • Search for node-mariasql on google – ufk Jan 09 '15 at 19:17

1 Answers1

1

The standard way in nodejs of dealing with async code is to provide a callback function as a last argument to your method and call it whenever your asynchronous finishes. The callback function standard signature is (err, data) - you can read more about here: Understanding callbacks in Javascript and node.js

Rewriting your code:

TuxDb.prototype.createIfNEDrinkCompany = function(drinkCompany, callback) { 
 this.client.query("insert into drink_company(drink_company_name) values(:drink_company) on duplicate key update drink_company_id=drink_company_id",
    {'drink_company' : drinkCompany})
    .on('result',function(res) {
    res.on('end',function(info){
        if (info.insertId > 0) {
                callback(null, row.drink_company_id);
        } else {
            this.client.query("select drink_company_id from drink_company where drink_company_name = :drink_company",{'drink_company' : drinkCompany})
                .on('result',function(res){
                    res.on('row',function(row){
                        callback(null, row.drink_company_id);
                    });
                });
        }
    });
    });

}

and then in the code calling your method

db.createIfNEDrinkCompany(drinkCompany, function(err, id){
    // do something with id here
})
Community
  • 1
  • 1
lukaszfiszer
  • 2,591
  • 1
  • 19
  • 13