0

I have several problems with return of queries.

Here, what I would like to do :

//If the email hasn't a good format
if(email_not_good_format())
   //I do something
else if(email_already_exists_in_mysql(email))
  //I do something


function email_already_exists_in_mysql(email){
connection.query('SELECT COUNT(*) AS nb FROM user WHERE emailUser = ' + connection.escape(email), function(err, rows, fields) {
        if (err) throw err;
        if(rows[0].nb == 0)
            return false;
        else
            return true;
    });
}

I saw on different posts callback function but it doesn't work for what I want to do.

4
  • for async work on JS take a look to promises. You have the Q library for node.js. Commented Jul 23, 2013 at 11:20
  • 2
    The call to .query is asynchronous. That's why you have to provide a callback. Have a look at this question to understand the problem and for a potential solution: stackoverflow.com/q/14220321/218196 (even though it's about Ajax, the same principals apply). Commented Jul 23, 2013 at 11:30
  • @TMichel r u getting any error. Commented Jul 23, 2013 at 11:40
  • You're attempting to perform a synchronous set of actions using an asynchronous language. You might like to start with some tutorials on JavaScript before attempting something a little more complex like database connections. Have you looked at any JavaScript tutorials? Commented Jul 23, 2013 at 12:15

1 Answer 1

1

I saw on different posts callback function but it doesn't work for what I want to do.

Yes it does, you just need to change the way you think about code. Instead of writing email_already_exists_in_mysql you should instead write a function called if_email_already_exists_in_mysql:

/* Executes callback if email
 * already exists in mysql:
 */
function if_email_already_exists_in_mysql (email,callback) {
    connection.query(
        'SELECT COUNT(*) AS nb FROM user WHERE emailUser = ' +
        connection.escape(email),

        function(err, rows, fields) {
            if(rows[0].nb != 0) {
                callback();
            }
        }
    )
}

Then instead of writing this:

//If the email hasn't a good format
if(email_not_good_format()) {
    //I do something
}
else if(email_already_exists_in_mysql(email)) {
    //I do something
}

you write it like this instead:

//If the email hasn't a good format
if(email_not_good_format()) {
    //I do something
}
else {if_email_already_exists_in_mysql(email),function(){
    //I do something
})}

Now, you may ask yourself, what if there is another else after that? Well, you need to modify the if_email_already_exists_in_mysql function to behave like and if...else instead of just and if:

function if_email_already_exists_in_mysql (email,callback,else_callback) {
    connection.query(
        'SELECT COUNT(*) AS nb FROM user WHERE emailUser = ' +
        connection.escape(email),

        function(err, rows, fields) {
            if(rows[0].nb != 0) {
                callback();
            }
            else if(else_callback) {
                else_callback();
            }
        }
    )
}

so that you can call it like this:

//If the email hasn't a good format
if(email_not_good_format()) {
    //I do something
}
else {
    if_email_already_exists_in_mysql(email),function(){
        //I do something
    },
    // else
    function(){
        //I do something else
    }
)}

You can write async code to do pretty much anything regular code can do only instead of returning a value you pass in a callback. Remember:

return in synchronous code == passing in callbacks in asynchronous code.

The code structure must therefore be different but as I demonstrated above the logic you want to implement can be exactly the same.

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

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.