Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved "Cannot read property 'parent' of undefined" #707

Closed
wants to merge 1 commit into from
Closed

Solved "Cannot read property 'parent' of undefined" #707

wants to merge 1 commit into from

Conversation

dokson
Copy link

@dokson dokson commented May 14, 2015

Solving this:

parse.js: at line 55
var oldParent = node.parent || node.root,
^
TypeError: Cannot read property 'parent' of undefined

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 98.63% when pulling 40bf415 on dokson:master into 2e82d09 on cheeriojs:master.

@davidchambers
Copy link
Contributor

Please add tests. :)

@dokson
Copy link
Author

dokson commented May 14, 2015

I'm sorry, but I'm noob to NodeJs.
Just solving this error got using https://www.npmjs.com/package/kickstarter-crawler
(https://github.com/ghostsnstuff/kickstarter-crawler)

@jugglinmike
Copy link
Member

Can you share the code that triggered the error? We need to understand the bug that you are trying to fix, and then we'll want to be sure it doesn't come back.

@inDream
Copy link

inDream commented Jul 30, 2015

@jugglinmike I've faced this bug with these conditions:
cheerio.load(null) or cheerio.load(undefined)
But this would not trigger the bug: cheerio.load(false)

@jugglinmike
Copy link
Member

@inDream I'm not sure those examples qualify as "bugs" because they describe undocumented usages of the load method. It might be nice if Cheerio threw a more descriptive error message, but generally speaking, it doesn't seem useful or intuitive to "load" values like null or undefined.

I might be wrong, though--can you give some context as to why you would like to use load in that way?

@inDream
Copy link

inDream commented Jul 30, 2015

@jugglinmike If load data from remote endpoint, network problem would cause this bug. I have no idea why this would happen as I haven't log the response content. Maybe there's other case would cause this bug but this is only one of the example and I haven't test this whether would fix the bug.

@jugglinmike
Copy link
Member

@inDream If the data source is unreliable, you might consider doing some explicit error checking in your application logic prior to invoking cheerio.load. That way you can recover or exit gracefully at the point of failure (and not bother attempting to parse/manipulate invalid data). Does that sound like a better approach to you?

@inDream
Copy link

inDream commented Jul 31, 2015

@jugglinmike This is a better approach but my concern is the error message is not clear.

@jugglinmike
Copy link
Member

@inDream Fair enough. The solution to your problem would take a completely different form than the patch provided here, though. Because it concerns an undocumented usage of the API, the fix is low-priority, but I would welcome a patch.

@chubaka
Copy link

chubaka commented May 4, 2016

Hi,
I am getting this error:
var oldParent = node.parent || node.root,
^

TypeError: Cannot read property 'parent' of undefined
Can you help?

@davidchambers
Copy link
Contributor

Could you provide a small, self-contained example, @chubaka?

@lfarquhar
Copy link

// I have the same issue. Here is an example.
var request = require('request');
var cheerio = require('cheerio');

var url = 'http:/www.msn.com';
request(url, function(err, resp, body){
$ = cheerio.load(body);
});

@davidbayo10
Copy link

This code is working for me.

const request = require('request');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');

function getDOM(url) {
  return new Promise(function (resolve, reject) {
    request({ uri: url, encoding: null }, function (err, res, html) {
      if (err) {
        reject(err);
      } else {
        html = iconv.decode(html, 'ISO-8859-1');
        resolve(cheerio.load(html));
      }
    });
  });
}

@fb55
Copy link
Member

fb55 commented Oct 31, 2016

Closing this as it is still missing a test case. If someone is able to provide one I am happy to reopen it.

@fb55 fb55 closed this Oct 31, 2016
@ghost
Copy link

ghost commented Nov 5, 2016

TypeError: Cannot read property 'parent' of undefined
at Function.exports.update (C:\Users\reza\Desktop\getomidname asasname\node_modules\cheerio\lib\parse.js:55:25)
at module.exports (C:\Users\reza\Desktop\getomidname asasname\node_modules\cheerio\lib\parse.js:17:11)
at Function.exports.load (C:\Users\reza\Desktop\getomidname asasname\node_modules\cheerio\lib\static.js:22:14)
at Request._callback (C:\Users\reza\Desktop\getomidname asasname\type1asasomid.js:94:29)
at self.callback (C:\Users\reza\Desktop\getomidname asasname\node_modules\request\request.js:186:22)
at Request.emit (events.js:107:17)
at ClientRequest. (C:\Users\reza\Desktop\getomidname asasname\node_modules\request\request.js:785:18)
at ClientRequest.g (events.js:199:16)
at ClientRequest.emit (events.js:104:17)
at Socket.emitTimeout (_http_client.js:543:10)

@ghost
Copy link

ghost commented Nov 5, 2016

help me

@fb55
Copy link
Member

fb55 commented Nov 5, 2016

Could you provide the HTML and the code that is causing that behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

10 participants