0

I want to save query result in a variable in my server using node.js and sqlite3, my code is like this:

function check(v_user, v_password,callback){
  stmt = db.prepare("select count from my_table where user = ? and password = ? ");
  stmt.bind(v_user, v_password);

  stmt.get(function(error,row){
    if(error){
      throw err;
      console.log("Entra en error");
    }
    else{
      if(row){
        callback(count); //count is 10
      }
      else{
        callback(0);
      }
    }
  });
}



var server = net.createServer(function(socket) {
  var my_var;
  my_user="Pepe";
  my_password="1234";

  function check(my_user, my_password, function(resultado){
    console.log(result); //result=10 (ok)
    my_var=result;
  }
  console.log(my_var); //not correct result

}

How can I have "result" in a varible outside of my function check? Thanks in advance.

Best regards;

Tecnico
  • 449
  • 2
  • 6
  • 17
  • 1
    Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Frxstrem Sep 30 '16 at 08:36
  • Put the `console.log` inside the callback? – DrakaSAN Sep 30 '16 at 08:40
  • Thanks for reply me DrakaSAN, inside the callback? I don't understand... I need to use my variable my_var outside of my function check. Inside the callback I only need to put "count" no? – Tecnico Sep 30 '16 at 08:47

0 Answers0