0
I am developing a test API using nodejs and Firebird and after a lot of struggle I was able to make the bank consultation work. However I am not able to make the function return the values that were located in the query. When I display via the.log() console I can view the results.
const client = require('../database/index');
var options = require('../database/config');
var resultado;
function setResultado(pResultado) {
resultado = pResultado;
}
function getResultado() {
return resultado;
}
const rows = async () => {
try{
try{
var pool = client.pool(5, options);
pool.get(function(err, db) {
try{
db.query("SELECT * FROM UCTABUSERS where UCIDUSER = 1", function(err, result) {
setResultado(result);
db.detach();
});
}catch{
console.log(err);
return err;
}
});
pool.destroy();
return getResultado();
}catch{
return 'Não foi possivel abrir a conexão';
}
}catch{
return "Não foi possivel conectar ao banco de dados";
}
};
const User = rows();
module.exports = User;
Anyone have any suggestions to solve my problem? Thanks in advance! (P.S.: I’m a beginner in javascript, I don’t know much yet)
There is the code where I call the route. But I even tested the route, when I put a string any works. The problem is that when I am using the Return getResulted(); already at the end of the Try, the getResulted() is returning undefined, it is as if within the pool function it existed and I was filled and outside was not.
router.get('/register', async (req,res) => {
try{
const user = await Users;
return res.send({user});
}catch{
res.status(400).send({error : 'Falha ao registrar'})
}
});
If on the.log console it prints the results maybe the problem is on route, it could put the code where you use rout.get to get back the query?
– Murilo Portugal
I did some other tests and modifications here and realized that the problem happens when I return the value of the query response. When you arrive at the Return getResult() line; the value of the result is undefined.
– Gustavomgu.developer
puts a
console.log(result);
shortly aftersetResultado(result);
and aconsole.log(getResultado());
beforereturn getResultado();
to see if it returns anything in the result and if it is really getting saved in setResult. But anyway, you could also replace thesetResultado(result);
forreturn result;
so you wouldn’t need to use an extra variable to store and a get/set. Kindly post the result.– Murilo Portugal
Good afternoon Murilo. I have already done this test, the problem is that for some reason the function is calling the get before the set(I believe that the operation that fills the set is taking too long), hence the values are not returned.
– Gustavomgu.developer
So friend, this is what I imagined, so I told you to put the Return result in place of the set. It came to test like this?
– Murilo Portugal
I made a modification that solved the problem, but I’m still not very satisfied. I put all the functions in the same file, with the exception of importing the connection library and the database settings, so I didn’t have to give a Return, I used the res.send( return ) function and I was able to access the data.
– Gustavomgu.developer
The point is that I wanted to keep the functions in separate files so that maintenance would be better. But I haven’t been able to do it yet, thanks anyway for the help.
– Gustavomgu.developer
Dude, you’re forgetting the detail that on the Node the input/output calls are non-blocking, because it runs all asynchronously. That is, if you send a search something in the database the program does not stand still waiting for the return of the database, it will continue running the rest of the code, and when the database responds the data to the program the Node picks them back by callback. That’s why I spoke from the beginning I said to use
return result
. But if you want to insist on making the program wait for the bank’s return look how to perform a synchronous function on Node.– Murilo Portugal
Although Node give you this option to run the synchronous function I do not recommend you to do this, because you will lose all the advantage and differential of Node,if you are going to leave for this solution look for another language that was developed already to work like this, like php or java. I suggest you read this article to get a better idea of how Node works and why.
– Murilo Portugal
I understood Murilo, as I said at the beginning I’m still beginner with Node js and I didn’t know that the technology is non-blocking but with his suggestion of Return I was able to recover the data. I just need to be able to separate the code more logically now for the sake of readability, ease of maintenance and reuse. Vlw for tips.
– Gustavomgu.developer