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

content.forEach is not a function when loading node that is not a root #1577

Closed
NeverBehave opened this issue Dec 21, 2020 · 1 comment · Fixed by #1580
Closed

content.forEach is not a function when loading node that is not a root #1577

NeverBehave opened this issue Dec 21, 2020 · 1 comment · Fixed by #1580

Comments

@NeverBehave
Copy link

NeverBehave commented Dec 21, 2020

I don't know whether is it an undocumented behavior or not, when trying to load a node that is not a root, cheerio will try to parse it as a list of nodes.

I believe this commit cause such break: 2e75c2a#diff-cc0e533610b6395aec57bad8971c3692fbf20950660cc5aa5014e58333a738baR28

Minimal Example

similar usage https://stackoverflow.com/questions/39044690/cheerio-map-strange-behaviour

const cheerio = require('cheerio');
const html = `
    <div class="test">
        <div></div>
        <div></div>
    <div>
`

cheerio.load(html)('.test').map((i, e) => {
    cheerio.load(e) // Break, not a root and not an array
})

If this is the desired behavior, what are the alternative ways to workaround this? Thanks!

@fb55
Copy link
Member

fb55 commented Dec 22, 2020

Thanks for the report! I've opened #1580 to fix this.

To help with your issue in the meantime: Typically, the function returned by cheerio.load is used to wrap elements in loops. For you, this would be:

const $ = cheerio.load(html);
$('.test').map((i, e) => {
    $(e).html() // `.html` as one example.
})

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

Successfully merging a pull request may close this issue.

2 participants