I recently encountered an issue with my Express API route that retrieves an artist's details along with an array of releases for that artist.
My current goal is to iterate over this array, extract each URL, and then make additional API calls to retrieve more data. I am trying to store the responses in an array which will be returned as a response to the front end.
However, I noticed that when I log the array, it appears to be empty.
I am utilizing the request-promise library from npm for this purpose.
Do you think my approach is correct? I suspect that I may need to ensure that all promises are resolved before proceeding with further execution, but I'm not entirely sure how to do so.
route.get('/search', (req, res) => {
const artist = req.query.artist || '';
let releases = [];
rp.get(`https://api.discogs.com/database/search?artist=${artist}&type=release&format=Vinyl&per_page=5&page=1`)
.then(({results}) => results.map(({resource_url}) => resource_url))
.then((resource_urls) => resource_urls.map((resource_url) => rp.get(resource_url).then((release) => releases.push(release))))
.then(() => console.log(releases))
.catch((err) => console.log(err))
});