My goal is to retrieve the data from the endpoint https://api.spotify.com/v1/me/top/tracks. I've setup an express server to handle routing and here is the endpoint.
app.get('/api/top/tracks', (req, res) => {
axios({
method: 'GET',
url: 'https://api.spotify.com/v1/me/top/tracks',
data: queryString.stringify({
limit: 30,
offset: 0,
time_range: 'medium_term'
}),
headers: {
'Authorization': 'Basic ' + encodedPayload,
'Content-type': 'application/json',
}
}).then(response => {
console.log(response.data)
res.json(response.data)
}).catch(error => console.log(error.response))
})
In another React functional component, I am trying to read the value in a useEffect, something like this.
const [topTracks, setTopTracks] = useState(tracks)
useEffect(() => {
async function getTopTracks() {
const response = await fetch('/api/top/tracks');
const json = await response.json();
setTopTracks(json);
}
getTopTracks();
}, []);
In all honesty I think I just don't understand the Response Promise object from API calls and am not sure how to handle the data I am attempting to retrieve.
EDIT: Appended .catch to the end of axios call. But now I receive a very long error response for an HTTP error 400 Bad Request.
