-1

I have this code: at the beginning of the file I initialized the variable (var loginState = false;) Why console.log give me false, although I change it to true

try {
        const client = new SimpleGraphClient(tokenResponse.token);
        const me = await client.getMe();

            sql.connect(config, async function (err){
                if (err) console.log(err);
                var request = new sql.Request();
                request.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`, async function (err, recordset){
                    if (err) console.log(err);
                    console.log(recordset);
                    if (recordset.recordset.length == 1) {
                        loginState = true;
                    } else {
                        loginState = false;
                    }
                    sql.close();
                });
            });
            console.log(loginState);
            if (loginState == true) {
                await turnContext.sendActivity({
                    text: 'Work',
                    attachments: [CardFactory.adaptiveCard(mainmenu)]
                });
            } else {
                await turnContext.sendActivity({
                    text: 'Dont work',
                    attachments: [CardFactory.adaptiveCard(internal_login)]
                });
            }            
    } catch (error) {
        throw error;
    }
  • 2
    Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – CertainPerformance Feb 04 '19 at 09:20

2 Answers2

0

Put that console.log() inside sql.connect()`, it works the way you are expecting.

sql.connect() is an asynchronous method, so by the time the console.log() happens the sql.connect() doesn't have changed the variable value yet.

loginState = false; // Statement 1

sql.connect(config, async function (err) { // Statement 2
    loginState = true;
})

console.log(loginState); // Statement 3

The order of execution of above will be,

Statement 1

Statement 3

Statement 2

So that's why it happens.


Change the code as follows for it to work fine.

try {
  const client = new SimpleGraphClient(tokenResponse.token);
  const me = await client.getMe();

  sql.connect(config, async function (err) {
    if (err) console.log(err);
    var request = new sql.Request();
    request.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`, async function (err, recordset) {
      if (err) console.log(err);
      console.log(recordset);
      if (recordset.recordset.length == 1) {
        await turnContext.sendActivity({
          text: 'Work',
          attachments: [CardFactory.adaptiveCard(mainmenu)]
        });
      } else {
        await turnContext.sendActivity({
          text: 'Dont work',
          attachments: [CardFactory.adaptiveCard(internal_login)]
        });
      }
      sql.close();
    });
  });
} catch (error) {
  throw error;
}

Hope this helps!

Community
  • 1
  • 1
bharadhwaj
  • 2,059
  • 22
  • 35
-1

The reason is because you are setting the value to variable inside a callback function : You have to await your query result. The way you are calling the query is not feasible.


Take a look at - https://www.npmjs.com/package/mysql2

Also change this part -

sql.connect(config, async function (err){
   if (err) console.log(err);
    var request = new sql.Request();
    request.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`, async function (err, recordset){
     if (err) console.log(err);
     console.log(recordset);
      if (recordset.recordset.length == 1) {
        loginState = true;
      } else {
        loginState = false;
      }
     sql.close();
    });
 });

Change this to something like this :

 try{
     let connection = await sql.connect(config);
     let query1 = await connection.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`);

      if (query1[0].recordset.length == 1) {
           loginState = true;
       } else {
           loginState = false;
       }
 }catch (err){
    // do something here
 }
Atish Shakya
  • 551
  • 6
  • 14