After requesting a Spotify access code using the POST method and storing the response in a variable, an attempt was made to access the token using the GET method immediately after.
{
angular.module('app')
.controller('GameController', function($http){
const $ctrl = this;
$http.post('/access-token').then(function(response){
$ctrl.tokenResponse = response.data;
$ctrl.myToken = $ctrl.tokenResponse.access_token;
console.log(response);
});
$http({
url: 'https://api.spotify.com/v1/tracks?ids=6rqhFgbbKwnb9MLmUQDhG6',
method: 'GET',
data: {
'Authorization': `Bearer ${$ctrl.myToken}`
}
}).then(function(response){
console.log(response.data);
$ctrl.tracks = response.data;
console.log($ctrl.tracks);
})
});
};
The issue arises when attempting to access the variables from the POST method using the GET method. The ${$ctrl.myToken} is showing as undefined in the "data":{"Authorization":"Bearer undefined"} section. However, logging the POST results before the GET method execution displays the access code correctly.
A solution is needed to make sure that the GET method can read the response from the POST method effectively.
All of this code resides in the controller component.