简体   繁体   中英

getting the result of query first and Then returning from function

I'm new to node.js. I'm trying to run this query and set a variable value based on the result and consequently return this set value.

However I've observed that my code my function exit it's scope first returning an undefined value and later on my query completes which is causing problem in my program.

How to handle this scenario where I want my query to run first set the variable value and than my function returns a value.

static  checkUser(email, password) 
  {
    console.log("Check user function logged");
    //----------------------------------------//
    //              Querying Password         //
    //----------------------------------------//
    console.log("Email in Par was : "+email);
    let sql = "SELECT password FROM members where email=?";
    console.log(sql);

    
    var auth;
    connection.query(sql,[email], (error, result, fields) => 
    {
      //-----------if Query has an error------------------//
      if (error) 
      {
        return console.error(error.message);
      }
      //-----------if Query result arry isn't empty----------//
      if (result.length > 0) 
      {
        if (result)
        {
          if(result[0].password===password)
          {
            //if resultant password is correct
            console.log("password was Correct");
            auth=true;
          }
          else
          {
            //if resultant password isn't correct
           auth=false;
          }
        }
      }
      else
      {
        auth=false;
      }
    });

    connection.end();
    console.log("function scope left");
    return auth;
  }

Javascript is asynchronous, auth is still undefined when the function returns in your code.

Try returning when you get the result, this can be in multiple places in your function.

 static checkUser(email, password) { console.log("Check user function logged"); //----------------------------------------// // Querying Password // //----------------------------------------// console.log("Email in Par was: " + email); let sql = "SELECT password FROM members where email=?"; console.log(sql); // var auth; connection.query(sql, [email], (error, result, fields) => { //-----------if Query has an error------------------// if (error) { connection.end(); return console.error(error.message); } //-----------if Query result arry isn't empty----------// if (result.length > 0) { if (result) { if (result[0].password === password) { //if resultant password is correct console.log("password was Correct"); connection.end(); return true; } else { //if resultant password isn't correct connection.end(); return false; } } } else { connection.end(); return false; } }); console.log("function scope left"); // return auth; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM