Brian
Brian

Reputation: 13573

Passing variables into callback function

I have a piece of code like this:

var guid = 'unique_guid';
con.query('SELECT guid FROM myDB.myTable WHERE guid = ?', guid, function(err, rows) {
    if(err) throw err;
    if(rows.length == 0) {
        console.log('new guid: ' + guid);
        // do more things which require guid
    } else {
        console.log('old guid: ' + guid);
        // do more things which require guid
    }
}

In order to avoid callback hell, I give the call back function a name and refactor it as the following:

var checkExistence = function(err, rows) {
    if(err) throw err;
    if(rows.length == 0) {
        console.log('new guid: ' + guid);       // guid can't be referenced here
        // do more things which require guid
    } else {
        console.log('old guid: ' + guid);       // guid can't be referenced here
        // do more things which require guid
    }
}
con.query('SELECT guid FROM myDB.myTable WHERE guid = ?', guid, checkExistence);

con is a connection created from node-mysql

Now my problem is that I can't reference guid in checkExistence(), and I don't want to make guid as a global variable.

Is it possible to get guid in checkExistence()?

Upvotes: 1

Views: 89

Answers (4)

Sanjeev Kumar
Sanjeev Kumar

Reputation: 457

    var checkExistence = function(err, rows, guid) {
    if(err) throw err;
    if(rows.length == 0) {
        console.log('new guid: ' + guid);       // guid can't be referenced here
        // do more things which require guid
    } else {
        console.log('old guid: ' + guid);       // guid can't be referenced here
        // do more things which require guid
    }
}
con.query('SELECT guid FROM myDB.myTable WHERE guid = ?', guid, checkExistence(err, rows, guid));

Upvotes: 0

Olivier Boissé
Olivier Boissé

Reputation: 18113

Maybe you could use the bind function,

var checkExistence = function(guid, err, rows) { ...

and call the method query like this

con.query('SELECT guid FROM myDB.myTable WHERE guid = ?', guid, checkExistence.bind(null, guid);

Upvotes: 0

jcubic
jcubic

Reputation: 66490

You can add guid as parameter and return a function:

var checkExistence = function(guid) {
    return function(err, rows) {
        if(err) throw err;
        if(rows.length == 0) {
            console.log('new guid: ' + guid);       // guid can't be referenced here
            // do more things which require guid
        } else {
            console.log('old guid: ' + guid);       // guid can't be referenced here
            // do more things which require guid
        }
    };
};
con.query('SELECT guid FROM myDB.myTable WHERE guid = ?', guid, checkExistence(guid));

Upvotes: 6

You can use Function.bind function for thet, like this:

var checkExistence = function(guid, err, rows) {
    if(err) throw err;
    if(rows.length == 0) {
        console.log('new guid: ' + guid);       // guid can't be referenced here
        // do more things which require guid
    } else {
        console.log('old guid: ' + guid);       // guid can't be referenced here
        // do more things which require guid
    }
}
con.query('SELECT guid FROM myDB.myTable WHERE guid = ?', guid, checkExistence.bind(null, guid));

Upvotes: 0

Related Questions