Problem getting value in NodeJS

0

I have a problem getting or filling a variable within a promise.

the code is:

var _TOKEN = '';

client.metodoGet(clientId, clientSecret)
  .then(function () {

    _TOKEN = client.authentications["XXXXXXX"].accessToken;
    console.log(_TOKEN);

this.token = _TOKEN;

      return _TOKEN;
  })
  .catch(function (response) {
    console.log(response);
  });

and I need to get the token to be able to send this variable to another file.

As you see I declare the variable outside the method, and I give it a value within the method, but when printing, I always get undefined.

I hope you can help me.

    
asked by Spyros Capetanopulos 28.08.2018 в 22:04
source

1 answer

0

Promises always return a value in the then you could try it like that

client.metodoGet(clientId, clientSecret)
  .then((resultado) => {
    console.log(resultado.authentications["XXXXXXX"].accessToken);
    this.token = resultado.authentications["XXXXXXX"].accessToken;
  })
  .catch((response) => {
    console.log(response);
  });
    
answered by 12.09.2018 в 09:16