Tuesday 29 August 2017

Why isn't my feed aggregator working?

I just started getting serious with NodeJS and as a learning project I am writing an app that combines RSS feeds into one aggregated feed.Problem is I'm having an issue with the asynchronous nature of returning the data and would appreciate a fresh pair of eyes on the matter.Here's the code in question:/* jshint esversion: 6 / / /dataMiner/dataMiner.js */(function () { 'use strict'; const //Node modules rssp = require('rss-parser'); const //Module methods dataMiner = { getData: function (res, feeds) { let data = []; feeds.forEach(function (feed) { rssp.parseURL(feed.uri, (err, parsed) => { if (err) { console.log(`There was an error with the feed: ${err}`); } let items = []; if (feed.itemLimit) { items = parsed.feed.entries.slice(0, feed.itemLimit); } else { items = parsed.feed.entries; } items.forEach((item) => { data.push({ provider: { name: feed.name, link: feed.uri }, title: item.title, date: item.isoDate, link: item.link }); }); }); }); console.log(data); res.json(data); } }; module.exports = dataMiner; }()); It works great if I only have one feed and I can put the res.json(data) inside the rssp() function, but when I pull that out so I can push multiple items from all the submitted feeds into one array I get an empty array, undoubtedly because the data variable is returned before anything is pushed to it.Any suggestions would be extremely helpful.Thanks!

Submitted August 29, 2017 at 10:52PM by recursiveCycle

No comments:

Post a Comment