0

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 returning in the body of each callback but it still doesn't work.

KylianMbappe
  • 1,896
  • 2
  • 15
  • 25
  • You need to make `handleSearch` return a Promise that resolves to `imgSrcs`. [This answer](https://stackoverflow.com/a/14220323/5648954) shows how you can do that, [this question](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) is also worth having a look at to see how you can do that. It is also worth having a look at the documentation for the `request` package to see if it has a promise based API already (and not just a callback one which you're currently using) – Nick Parsons Jan 02 '23 at 13:06

0 Answers0