Cannot read property 'parent' of undefined - Node.js

Xie Zhijie picture Xie Zhijie · Jan 2, 2015 · Viewed 14.8k times · Source

I was trying to employ superagent and cheerio to crawl data from kijiji for learning, while I got an error of "Cannot read property 'parent' of undefined". (I tried craigslist which was fine). It happened after I got the correct results "object". It seems something wrong with cheerio. Does anyone have any idea about this? Appreciate for your help in advance.

[ERROR] 17:30:24 TypeError
TypeError: Cannot read property 'parent' of undefined
    at Function.exports.update (C:\Users\zhi_xie\nodejs\neomovie\node_modules\ch
eerio\lib\parse.js:55:25)
    at module.exports (C:\Users\zhi_xie\nodejs\neomovie\node_modules\cheerio\lib
\parse.js:17:11)
    at Function.exports.load (C:\Users\zhi_xie\nodejs\neomovie\node_modules\chee
rio\lib\static.js:19:14)
    at C:\Users\zhi_xie\nodejs\neomovie\routes\crawlerusers.js:35:27
    at Request.callback (C:\Users\zhi_xie\nodejs\neomovie\node_modules\superagen
t\lib\node\index.js:746:30)
    at Request.<anonymous> (C:\Users\zhi_xie\nodejs\neomovie\node_modules\supera
gent\lib\node\index.js:135:10)
    at Request.emit (events.js:95:17)
    at ClientRequest.<anonymous> (C:\Users\zhi_xie\nodejs\neomovie\node_modules\
superagent\lib\node\index.js:921:12)
    at ClientRequest.emit (events.js:95:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1692:21)

Here is the codes:

  var superagent = require('superagent');
  var cheerio = require('cheerio');
  var url = require('url');
  var kijijiUrl = 'http://www.kijiji.ca/b-immobilier/ville-de-montreal/c34l1700281?ad=wanted';

  superagent.get(kijijiUrl).end(function (err, res) {
    if (err) {
      return console.error(err);
    }
    var topicUrls = [];
    var $ = cheerio.load(res.text);
    // get all links in that page
    $('div.container-results').children('table').each(function (idx, element) {
      var $element = $(element);
      var href = url.resolve(kijijiUrl, $element.attr('data-vip-url'));
      topicUrls.push(href);
    });

    for(i = 0; i<topicUrls.length; i++){
      superagent.get(topicUrls[i])
        .end(function (err, sres) {
          //normal err handler
          if (err) {
            return next(err);
          }
          var $ = cheerio.load(sres.text);
          var object = {};
          $('table.ad-attributes').find('tr').not('.divider').each(function(i, element){
            var titles = $(element).children('th').text().trim();
            var values = $(element).children('td').text().trim();          
            object[titles] = values;
          });
          console.log(object);               
      });        
    }
  })  

Answer

Luca Steeb picture Luca Steeb · Mar 27, 2015

As chrki already said, cheerio.load() is called with an empty value like null or an empty string. Check whether the value contains something before you call .load():

if (res.text) {
    var $ = cheerio.load(res.text);
} else {
    // do something else
}