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

prop('src'): TypeError: Cannot read properties of undefined (reading 'type') #2552

Closed
Markus-Rost opened this issue May 24, 2022 · 3 comments · Fixed by #2557
Closed

prop('src'): TypeError: Cannot read properties of undefined (reading 'type') #2552

Markus-Rost opened this issue May 24, 2022 · 3 comments · Fixed by #2557

Comments

@Markus-Rost
Copy link

I'm using 1.0.0-rc.11. Trying to get the src property of an Cheerio without any elements throws a TypeError.

$('.infobox').find('img').filter( (i, img) => {
	img = $(img).prop('src')?.toLowerCase();
	return img?.startsWith('https://');
} ).first().prop('src');

The .prop('src') on the .first() throws an error if the filter() returned an empty list.

file:///home/wikibot/bots/discord-wiki-bot/node_modules/domelementtype/lib/esm/index.js:29
    return (elem.type === ElementType.Tag ||
                 ^

TypeError: Cannot read properties of undefined (reading 'type')
    at isTag (file:///home/wikibot/bots/discord-wiki-bot/node_modules/domelementtype/lib/esm/index.js:29:18)
    at isTag (file:///home/wikibot/bots/discord-wiki-bot/node_modules/domhandler/lib/esm/node.js:224:12)
    at LoadedCheerio.prop (file:///home/wikibot/bots/discord-wiki-bot/node_modules/cheerio/lib/esm/api/attributes.js:155:22)
    at file:///home/wikibot/bots/discord-wiki-bot/functions/parse_page.js:379:18
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.1.0

This worked without an error in 1.0.0-rc.10.

Also there is a ' missing in the description.
image

Markus-Rost added a commit to Markus-Rost/discord-wiki-bot that referenced this issue May 24, 2022
@fb55
Copy link
Member

fb55 commented May 25, 2022

Thanks for the report! I had a look at all props, and have a comprehensive fix coming in with #2557.

@Kikobeats
Copy link

Hey @fb55, can you release a new rc version? latest is broken because prop throws an error under no presence 🙂

btw, thanks for this, I didn't note about .prop until now.

being the output the same, will be .prop prefer over .attr? asking form a perf point of view

@fb55
Copy link
Member

fb55 commented May 28, 2022

.attr is doing less work and will always be faster.

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.

3 participants