Cheerio: content.ForEach is not a function

Created on 24 Dec 2020  路  6Comments  路  Source: cheeriojs/cheerio

Hello, I am a developer of a Homebridge plugin, and I am having a user report this error:

[12/23/2020, 4:09:41 PM] TypeError: content.forEach is not a function
    at parse (/homebridge/node_modules/homebridge-adt-pulse/node_modules/cheerio/lib/parse.js:38:15)
    at Function.exports.load (/homebridge/node_modules/homebridge-adt-pulse/node_modules/cheerio/lib/static.js:39:14)
    at /homebridge/node_modules/homebridge-adt-pulse/api.js:598:39
    at arrayMap (/homebridge/node_modules/homebridge-adt-pulse/node_modules/lodash/lodash.js:639:23)
    at Function.map (/homebridge/node_modules/homebridge-adt-pulse/node_modules/lodash/lodash.js:9580:14)
    at Request._callback (/homebridge/node_modules/homebridge-adt-pulse/api.js:597:28)
    at Request.self.callback (/homebridge/node_modules/homebridge-adt-pulse/node_modules/request/request.js:185:22)
    at Request.emit (events.js:315:20)
    at Request.<anonymous> (/homebridge/node_modules/homebridge-adt-pulse/node_modules/request/request.js:1154:10)
    at Request.emit (events.js:315:20)
[12/23/2020, 4:09:41 PM] Got SIGTERM, shutting down Homebridge...

More details in this issue

Most helpful comment

sample from your code where it happens

...
const $ = cheerio.load(body);
const sensors = $('#orbSensorsList table tr.p_listRow').toArray();

const output = _.map(sensors, (sensor) => {
  const theSensor = cheerio.load(sensor);
  const theName = theSensor('a.p_deviceNameText').html();
  ...
})
...

variable sensor is single object (html node)

older version wraps it into array

All 6 comments

Cheerio.load expects there to be array. Older versions was there condition for checking, if it is not array. Seems like it is lost due to optimizations.

Yeah, the input is an array. I don't get why it's having this error.

sample from your code where it happens

...
const $ = cheerio.load(body);
const sensors = $('#orbSensorsList table tr.p_listRow').toArray();

const output = _.map(sensors, (sensor) => {
  const theSensor = cheerio.load(sensor);
  const theName = theSensor('a.p_deviceNameText').html();
  ...
})
...

variable sensor is single object (html node)

older version wraps it into array

Oh haha! Totally missed that actually.

Screen Shot 2020-12-24 at 3 29 21 PM

Hmm could be the types need to be changed also, but yes, your suggestion works.

Thanks for the report! This was already fixed in https://github.com/cheeriojs/cheerio/commit/0855be618c327ab5adab0dedcc008e6e8c84eaf1, which should be part of a new release soon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

askie picture askie  路  4Comments

rajkumarpb picture rajkumarpb  路  3Comments

dandv picture dandv  路  5Comments

becush picture becush  路  3Comments

AlbertoElias picture AlbertoElias  路  4Comments