0

I'm using express framework to grab links from some webpage, and I add this links to Array. I'm using async to print final result but when I print my array I gets list of objects.

Result of collate function:

Finall: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Objec
t] 

How can I iterate of this array of objects? This is my code:

router.route('/send')
  .post(function(req, res){
      async.series([ function(callback){
            var url = req.body.url;
            var items = [];

            console.log(url);
            if(url.length>=1) {

              if (isURL(url)) {
                console.log('OK');
                res.sendStatus(200);

                request(url, function(err, resp, body){

                  $ = cheerio.load(body);
                  links = $('a.offer-title');
                  $(links).each(function(i, link){
                     items[i] = new itemParam($(link).text(),12)
                  });
                  callback(false, items);

                });

              } else {
                errorHandling(res, 401,"Invalid url");
              }
            }else{
                errorHandling(res, 401,"Invalid url");
            }
        }
      ],
        /*
         * Collate results
         */
        function(err, p) {
          console.log("Finall: " + p[0]);

       }
      );
  });
lukassz
  • 3,135
  • 7
  • 32
  • 72

1 Answers1

2

here's one simple way to do this:

const request = require('request'),
      cheerio = require('cheerio')

const scrape = (url) => {
    return new Promise((resolve, reject) => {
        let links = [] // collect all links here
        request(url, (err, res, body) => {
            if (err) {
                return reject(err)
            }
            let $ = cheerio.load(body)
            $('a').each(function () { // use any selector you like
                links.push($(this).attr('href')) // ...and extract whatever you like
            })
            resolve(links)
        })
    })
}

scrape('http://google.com?q=javascript')
  .then(links => {
      // handle links
      console.log(links)
  })
  .catch(err => {
      // handle error
  })
Kuba Kutiak
  • 92
  • 1
  • 3