0

I am new to javascript so please forgive me if this is obvious, but I am making a request and trying to access the results in another function, but it always comes back as undefined. When I print the response within the original function it is correct, but always undefined when I try to print it elsewhere. Am I returning it incorrectly?

This is my method making the API call:

const authToken = () => {

var options = {
    method: 'POST',
    url: url,
    headers:
    {
        'cache-control': 'no-cache',
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    form:
    {
        j_username: config.auth.username,
        j_password: config.auth.password,
    }
};

request(options, (error, response, body) => {
    if (error) throw new Error(error);

    authToken = response.headers['set-cookie'][0].split(";")[0];
    return authToken;
});

};

And trying to call it here always returns undefined:...

    router.get('/', (req, res) => {
        token = authToken()
    });
buffcat
  • 265
  • 2
  • 5
  • 17
  • 1
    The function is not returning anything. Look into promises. The referenced link has lots and lots of info. – trincot Jul 26 '19 at 17:24
  • bearerToken is never assigned and as @trincot said, you're not returning anything in authToken, its returning in request – John Jul 26 '19 at 17:25

0 Answers0