Cannot define extra entry fields to fetch
ryan-hmd opened this issue · comments
Hi,
The RSS feed I fetch includes, for each item, an illustration image of the published article. As this field is not provided by default, I have tried to define it in the parser options as explained here in the documentation, but I get the following error when executing my script:
TypeError: Cannot read properties of undefined (reading '@_url')
Here is the definition of my options (I use typescript):
const options = {
getExtraEntryFields: (entryData: any /* What is the expected type ?? */) => {
const { enclosure } = entryData
return {
enclosure: {
url: enclosure['@_url'], // enclosure is undefined ...
type: enclosure['@_type'], // enclosure is undefined ...
}
}
}
}
const rss = await extract(url, options)
I'm new to using RSS feeds, can you help me understand the error and get to the point?
Well... I'm a bit embarrassed, but I realized my mistake and solved the problem a few minutes after posting the issue. I naively used the keys from the documentation example, whereas the keys I need to extract are named differently... It's as simple as that.
The corrected script:
const options = {
getExtraEntryFields: (entryData: any) => {
const enclosure = entryData['media:content'] // the key I wanted is named 'media:content'
if (!enclosure) return {}
return {
enclosure: {
url: enclosure['@_url'],
type: enclosure['@_type']
}
}
}
}
Sorry for opening up this non-issue.
@ryan-hmd yes, this lib converts XML document into a JS Object looks like image below. The full object (just call it feedData
) will be used for getExtraFeedFields()
, while the child items (just call them feedEntry
) will be used for getExtraEntryFields()
.
So getExtraFeedFields
can access the whole data, while getExtraEntryFields
can help to manipulate every entry.