0

I would like to return if true or false the user exists by retrieving the information of a user, if there is information then we return true, otherwise we return false. The problem is that in my res.on('end') I recover the user data well, but it is impossible to recover his data outside of this function. Do you have an idea ?

this.api.getEnv() = http (dev) or https (prod)

        GetUserById = function (id) {

        const req = this.api.getEnv().request(this.api.ApiHeader('/user/' + this.PlayerIdentifiers(id, "license"), 'GET'), res => {

            let body = '';
            res.on('data', d => {
                body += d;
            });

            res.on('end', function () {
                const user = JSON.parse(body);
                console.log(user);
            });

            if (user) {
                return true;
            } else {
                return false;
            }
        });
BADZY
  • 51
  • 6
  • The constant `user` is created inside the `res.on('end')` function, so it is not visible outside that function. To make it work, maybe you need to put `res.send` inside the `res.on('end')` function? Something like `res.send(user ? true : false);` just after `console.log(user);` – jeojavi Oct 11 '20 at 22:20
  • Hello, thanks you for your response. I come to a new error: res.send is not a function. – BADZY Oct 12 '20 at 11:02
  • @Evert, thank you very much, that's exactly what i needed. – BADZY Oct 12 '20 at 17:16

1 Answers1

-2
res.on('end', function () {
  const user = JSON.parse(body);
  console.log(user);
  if (user) {
    return true;
  } else {
    return false;
  }
});
codebrane
  • 4,290
  • 2
  • 18
  • 27