oskll
oskll

Reputation: 47

Undefinied/Empty variable in Javascript functions

I have some trouble with the following code. I have an api which can fetch differents URL's. The actions will be different according to the URL I have, so I did a function to detect the site I'm going to make actions on. It looks like this at the moment :

router.get('/:url(*)', async function (req, res) {

       let jsonObject;

       jsonObject= await chooseScrapperFromUrl(req.params.url);

       res.json(jsonObject);
}

async function chooseScrapperFromUrl(url) {

    let jsonObject= '';

    if (url.includes('www.someSite.com')) {
        jsonObject= await someSiteScrapper(url);
    }

    return jsonObject;

}

async function someSiteScrapper(url) {
      let jsonObject= '';

      try {
              //creating the jsonObject variable here ...
              //jsonObject = //some cheerio calls here but nothing more
        request(url, (error, response, html) => {
        if (!error && response.statusCode == 200) {

            const $ = cheerio.load(html);

            $('.pb-center-column').each((i, el) => {
                const productName= $(el).find('#something').find('span').text().replace(/\s\s+/g, ' ').trim();
                jsonObject+= '{ "name": "' + productName;

                let price= $(el).find('.price').text().trim();

                jsonObject+= '"price": ' + parseFloat(price) + ', ';

            jsonObject+= '"originUrl": "' + url + '"}';
      } catch (err) {
        console.log(err);
      }

      return jsonObject;
}

And you see the variable I have jsonObject is created in the someSiteScrapper function and should be returned to my route which will send it to my front-end client. I checked and the jsonObject is created in the try/catch loop but once I'm out of it it's empty or Undefined. I don't understand why.

Can you help me pls ?

Upvotes: 0

Views: 73

Answers (1)

BENARD Patrick
BENARD Patrick

Reputation: 30975

Try with a promise... It's due to the fact you return the value before the changes....

async function someSiteScrapper(url) {
      return new Promise((resolve, reject) => {
      let jsonObject= '';

      try {
              //creating the jsonObject variable here ...
              //jsonObject = //some cheerio calls here but nothing more
        request(url, (error, response, html) => {
        if (!error && response.statusCode == 200) {

            const $ = cheerio.load(html);

            $('.pb-center-column').each((i, el) => {
                const productName= $(el).find('#something').find('span').text().replace(/\s\s+/g, ' ').trim();
                jsonObject+= '{ "name": "' + productName;

                let price= $(el).find('.price').text().trim();

                jsonObject+= '"price": ' + parseFloat(price) + ', ';

                jsonObject+= '"originUrl": "' + url + '"}';
            });

            resolve(jsonObject);
      } catch (err) {
        reject(err);
      }


      });
}

Upvotes: 2

Related Questions