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()
});