Hello, im trying to make an API GET in Node.js, but i think the data is being read before it should be
Here is my code
app.get('/endpoint', async (req, res) => {
try {
var online = await exibir()
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
const lgtaon = 'is_live:'
if (online == 'false'){
res.send( lgtaon +'false')
console.log('lg ta offline')
} else {
res.send(lgtaon +'true')
console.log("lg ta online");
}
} catch(err){
console.error(err)
res.status(500).send("Erro ao verificar status")
}
})
So, if the streamer is Online, it should print
console.log('lg ta offline')
otherwise, it would be printed the following message:
res.send(lgtaon +'true')
console.log("lg ta online");
}
But this is the answer I get:
The answer ‘TÁ ON? false’ is the real status about the streamer
and the answer ‘lg ta online’ is the wrong answer called by the If…then boolean I set in API GET
Idk why the difference if Im using the same variable that I use on exibir()
Also, the function exibir() is outside my API GET, and Ive already tried to use it inside the API GET, but it also doesnt work
Well to start debugging while the online status is not what you expect, console log the value of online and ensure that it is what you think it is. If that’s incorrect, look at the exibir function and find out why it’s incorrect.
so, the problem is that i was writing the logic right, but i wasnt returning the result of exibir function, then i got undefined, what makes total sense
so i returned the result and the app.get worked as i wanted to