Read the statement by Michael Teeuw here.
Problems with rss
-
This is my first attempt at a module using a node_helper.js. My goal is to get an rss and output all rss items on the mirror. I have got the basics working, which is a function in node_helper.js which sends items over to the module js file:
loadFeed: function(url) { var items = []; items.push( { title: 'test title', description: 'test description' } ); this.sendSocketNotification('ITEMS', { items: items }); },
Output:
But when I try to read and parse an rss, I get nothing… Code:
const FeedMe = require('feedme'); const http = require('http'); ... loadFeed: function(url) { var items = []; items.push( { title: 'before', description: 'before' } ); http.get('http://www.npr.org/rss/rss.php?id=1001', (res) => { var parser = new FeedMe(); parser.on('title', (title) => { items.push( { title: 'feedtitle', description: 'feedtitle' } ); }); parser.on('item', (item) => { items.push( { title: 'feeditem', description: 'feeditem' } ); }); res.pipe(parser); }); items.push( { title: 'after', description: 'after' } ); this.sendSocketNotification('ITEMS', { items: items }); },
Output:
“ls node_modules” show that both feedme and http is installed. No relevant errors in .pm2/logs/mm-error-0.log or mm-out-0.log.
So I tried with another rss parser:
const parser = require('rss-parser'); ... loadFeed: function(url) { var items = []; items.push( { title: 'before', description: 'before' } ); parser.parseURL('http://www.npr.org/rss/rss.php?id=1001', function(err, parsed) { parsed.feed.entries.forEach(function(entry) { items.push( { title: 'entrytitle', description: entry.title } ); }); }); items.push( { title: 'after', description: 'after' } ); this.sendSocketNotification('ITEMS', { items: items }); },
Same output as above. No log errors. And rss-parser is installed in node_modules. Also this last code works fine in npm RunKit:
https://runkit.com/embed/q4ucjhbbzpw3Any ideas what is failing? Or how to debug?
-
@retroflex I didn’t look inside deeply, but I think it might be caused by asynchronous problem.
I suspect sendNotification is performed before parsing rss(item.push)
Test it withconsole.log()
before sendNotification and item pushing. -
@Sean Thanks! Logging shows your suspicions were correct:
MYMODULE Thu Nov 23 2017 09:43:15 GMT+0100 (CET): start of method MYMODULE Thu Nov 23 2017 09:43:15 GMT+0100 (CET): end of method MYMODULE Thu Nov 23 2017 09:43:16 GMT+0100 (CET): parsed an rss entry MYMODULE Thu Nov 23 2017 09:43:16 GMT+0100 (CET): parsed an rss entry MYMODULE Thu Nov 23 2017 09:43:16 GMT+0100 (CET): parsed an rss entry ...
Now I will try to find out if the rss parsers have a finished callback and put the sendSocketNotifications in there…
-
To follow up… I got it to work by comparing parsed.feed.entries.length to the number of parsed entries, and when all entries were passed I called a callback that sent the notifications.
I’m new to javascript so there’s still a lot to learn.