S
S
serNevajno2019-07-11 12:05:33
Node.js
serNevajno, 2019-07-11 12:05:33

How to work properly in Node js?

Hello uv. community. Please tell me, using an example, how to work correctly in Node js.
Here is the code:

var config = require('./config');
var query = require('./mysql').query;

var res;
query('SELECT test FROM test WHERE id=1 LIMIT 1').then(function (result) {
  if(result){
    res = result[0].test;
    console.log("result = "+res);
  }
}).catch(function (err) {
  console.log('Error');
  console.log(err);
});
console.log("result out = "+res);

Result:
result out = undefined
result = web
I understand that the output line to the console is processed before the result is entered into the variable, but I don’t understand how to implement everything correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-07-11
@serNevajno

So you have everything right and implemented. There are no errors, everything works.
A bit of telepathy:
If you want to work with res - work inside then.
If you want to do something while the request is coming, you do it where you have "result out".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question