question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'find', 'attr', possibly more methods are undefined in .map 'this' context.

See original GitHub issue

Calling this.find('...'), this.attr('...') throws a TypeError: Object #<Object> has no method ... error when called in the context of .map.

Example:

var $ = require('cheerio').load(
    '<ul>'
  +   '<li><a href="/1">1</a></li>'
  +   '<li><a href="/2">2</a></li>'
  +   '<li><a href="/3">3</a></li>'
  +   '<li><a href="/4">4</a></li>'
  + '</ul>'
);

// Works
console.log($('li').eq(0).find('a').attr('href'));

// Fails with:
//   return this.find('a').attr('href');
//               ^
// TypeError: Object #<Object> has no method 'find'
console.log($('li').map(function () {
  return this.find('a').attr('href');
}));

This worked in cheerio 0.12.4 and fails in 0.13.1

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jugglinmikecommented, Jan 16, 2014

Hi Aleksey,

The change you’ve reported is intentional because it more closely mimics jQuery. Just like in jQuery, the context of the callback function is the node. If you’d like to use the Cheerio API, then you can create a new Cheerio object with the node:

 console.log($('li').map(function () {
-  return this.find('a').attr('href');
+  return $(this).find('a').attr('href');
 }));

Does this work for you?

0reactions
matthewmuellercommented, Jan 16, 2014

yah, there’s lots of things i’d like to change about jquery. check out the client-side component/dom for some of those ideas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: Object is possibly 'undefined' - Stack Overflow
There are two ways to that I can think of to get rid of the error. The first way I can think of...
Read more >
Maps JavaScript API | Google Developers
For vector maps, this method sets the map's tilt and heading to their default zero ... If the map is not yet initialized...
Read more >
How to Read React Errors (fix 'Cannot read property of ...
Cannot read property `map` of undefined. In this post we'll talk about how to fix this one specifically, and along the way you'll...
Read more >
CoffeeScript
The JavaScript arguments object is a useful way to work with functions that accept variable numbers of arguments. CoffeeScript provides splats ... ,...
Read more >
OpenLayers v7.2.2 API - Class: Map
The constructor places a viewport container (with CSS class name ol-viewport ) in the target element (see getViewport() ), and then two further...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found