0

so here's the issue, everything inside the con.query(query, function (err, result, fields) is never called. So this is the part of code that is about verifying that when the user is signing up, the email that he took is not already taken. argv[0] contains the email that the user typed (it's not wrong, I debugged it). So I obviously created two accounts with the same email but the second one is never flagged (the function is continuing instead of returning "userexists").

var mysql = require('mysql'); 

var con = mysql.createConnection(Credentials). // 👈 The connection is established, it's not a problem.

var query = "SELECT id FROM accounts_data WHERE email = '" + argv[0] + "'";
var r;
con.query(query, function (err, result, fields) {
  if (err)
    return "failed";
  if(result != undefined) 
    return "userexists" // The if is never checked
});

Thank you.

EDIT: Hello everyone, so the console.log(results.length) is printing the right result, but how can I give the result to r? Because the last console.log(r) is still printing 0.

var r = 0;
var sql = 'SELECT * FROM accounts_data WHERE email = ' + con.escape(argv[0]);
con.query(sql, function (error, results, fields) {
if (error) throw error;
console.log(results.length); // Right result
r = results.length; // Not giving the result to r
});
console.log(r); // Not the right result
5
  • 1
    I highly suggest you read mysql's README section titled, Escaping query values... Commented Jan 2, 2019 at 5:52
  • use con.query('SELECT id FROM accounts_data WHERE email = ?', [argv[0]], (err, res) => { ... }) Commented Jan 2, 2019 at 5:54
  • Given query() is asynchronous, you cannot return the results to anything. Also, you should be checking result.length Commented Jan 2, 2019 at 5:58
  • There are two things need to change. 1. return will not work inside the callback. 2. if the result is null then result != undefined will get false. So, you have to change the line to if(result) Commented Jan 2, 2019 at 6:51
  • Now I just can't assign my local variable to result Commented Jan 2, 2019 at 18:48

1 Answer 1

1

Try using Promise's, they'll help get away from 'callback-hell'

const userExists = async emailAddress => new Promise((resolve, reject) => {
  con.query(`
     SELECT EXISTS(
       SELECT id FROM accounts_data WHERE email = ?
     );
    `,
    [emailAddress],
    (err, result) => {
      if (err) { reject(err); }

      resolve(result);
    }
  )
);

await userExists('[email protected]') // => boolean
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank you for your reply, I'm still a beginner with JS (I coded with other languages I'm not 100% new). How can I retrieve the data returned by await userExists('[email protected]')
userExists('[email protected]') returns a promise, which can be accessed either by userExists('[email protected]').then(exists => { /* use it here*/ }) or const exists = await userExists('[email protected]'); if (exists) { /* do something */ } but await can only be used inside of an async function use this for reference; developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.