I'm trying to build an API in NodeJS that uses the request
package as well as cheerio
. It scrapes images from a URL and I'd like to use the image src URIs stored in an array and send them back to the user (res.json()
).
I can't seem to get the imgSrcs
out of the request()
callback body.
let result = [];
const handleSearch = async searchQuery => {
const baseUrl = `url/${searchQuery}`;
request(baseUrl, (err, resp, html) => {
const imgSrcs = []; // <------ I want to return the stuff stored in here at handleSearch() level
if (!err && resp.statusCode === 200) {
const $ = cheerio.load(html)
$('img').each((_, image) => {
let img = $(image).attr('src')
imgSrcs.push(img)
});
}
result = imgSrcs; // <------ This is the super crude, semi-working way of doing it (only way that has worked so far, but it only displays the correct result when refreshing the page twice not once
})
return result;
}
Then in my index.js
file, I have the following that uses the above code:
const searchData = await handleSearch(searchQuery);
res.json(searchData)
How do I get the imgSrcs
array out of the nested inner callbacks so I can return it at the top level handleSearch()
function? And then I can use this in index.js
?
I have tried assigning the request()
to a variable but it gives me a convoluted JSON body with no way of getting the data inside it. I've also tried return
ing in the body of each callback but it still doesn't work.