How set boolean logic in node.js based on Twitch API?

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:

image

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

Any help would be greatly aprecciated
Thanks

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.

But exibir function is correct and the boolean isnt responding properly, it should respond the same as exibir, but is not

it should respond the same because in the res.send i want to show the answer in the json

so i tried:

  • instead of using exibir(), creating the same function inside the app.listen and call the varibles directly for if…then
  • embrace all the functions using the app.listen and calling exibir() as parameter for if…then
  • call exibir() before if…then iside and outside (not at the same time) app.listen

but it didnt work with neither of what i tried

do you have any idea of another possibility?

I did what you said and my const online is coming undefined

how do I call the result of another function to use it inside my app.listen?

I’ve trid to use .then and after that I tried with await, but its the same answer: undefined

   app.get('/endpoint', (req, res) => {
      exibir().then((on) => {
          res.statusCode = 200;
          res.setHeader('Content-Type', 'text/plain');
          const lgtaon = 'is_live:'
          console.log('on é ' + on)
          if (on == false) {
              res.send(lgtaon + 'false')
              console.log('lg ta offline')
          } else {
              res.send(lgtaon + 'true')
              console.log("lg ta online");
          }
      });
  });
  

its the second time u help me hahaha tks

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

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.