S
S
Sergey2018-02-18 16:05:30
MySQL
Sergey, 2018-02-18 16:05:30

Waiting for response from mysql?

Please help, I googled a lot, tons of code in questions and answers, I can’t understand anything, how to force me to wait for a response from mysql? Client authorization occurs through cookies and a request to the database. The code continues to run, a undefined. console.log(results.length) will work, but then. How to solve this problem, preferably with an example on this code.

var a;
con.query("SELECT count(*) FROM `users` ...", function(error, results, fields){
  if (error) throw error;
  console.log(results.length); // количество совпадений
  a = results.length;
});

if(a ...) // undefined
  ...

thanks again Abcdefgk

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2018-02-18
@chelovekmuravei

con.query is asynchronous, the code written after it (if (a)...) will be executed immediately, without waiting for the request to be executed, therefore the value is not set, and what is executed after the request is executed is described in the callback, and do everything there actions with query results

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question