I'm currently trying to make a node.js script to read a couple of RSS feeds from a JSON file (config.json), check everyone and send new items to it's specific webhook for Discord.
The config file is the following:
[
{
"name": "r/aww",
"avatar": "https://example.com/avatar.jpg",
"rss_url": "https://www.reddit.com/r/aww/top/.rss",
"webhook_url": "https://discord.com/api/webhooks/..."
},
{
"name": "r/ImaginaryLandscapes",
"avatar": "https://example.com/avatar.jpg",
"rss_url": "https://www.reddit.com/r/ImaginaryLandscapes/top/.rss",
"webhook_url": "https://discord.com/api/webhooks/..."
}
]
And the code that i attempted to do, following sync examples from rss-parser npm package, is the following:
let Parser = require('rss-parser');
let parser = new Parser();
const config = require("./config.json");
config.forEach(element => {
parser.parseURL(element.rss_url, function(err, feed) {
console.log(feed);
feed.items.forEach(function(entry) {
console.log(entry.title + ':' + entry.link);
});
});
});
But trying to run it throws the following error:
undefined
/mnt/d/misc/feedchecker/script.js:28
feed.items.forEach(function(entry) {
^
TypeError: Cannot read property 'items' of undefined
at /mnt/d/misc/feedchecker/script.js:28:9
at Timeout.setTimeout [as _onTimeout] (/mnt/d/misc/feedchecker/node_modules/rss-parser/lib/utils.js:63:29)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
What I'm doing wrong on this code? Please note that i still didn't wrote the webhook sending part, I'm focused still on fetching the feeds first.
I suspect this specific error was caused because i was (without knowing) using Node.JS v10 (that is the version that is shipped by apt by default for WSL's Ubuntu).
Reinstalling Node.JS using NodeSource repository gives me the version 16 that is able to run the code without issues.