1

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()?

2
  • Callback will be function(){ checkExistence(guid); } and accept guid as argument.. Commented Feb 18, 2016 at 10:56
  • Why not just add a parameter? Commented Feb 18, 2016 at 10:57

4 Answers 4

6

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :-). I'm new to javascript.
0

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));

Comments

0

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);

Comments

0
    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));

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.