danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 192 forks source link

Q: integration with express template #161

Closed marti1125 closed 8 years ago

marti1125 commented 8 years ago

I would like to print all titles but only get the last

     feedparser.on('readable', function() {

           var stream = this
          , meta = this.meta
          , item;

         var  titles = []
            while (item = stream.read()) {

               titles.push(item.title)

            }

           res.render('index', { title: 'Express', data: titles });

       });
danmactough commented 8 years ago

@marti1125 You need to put var titles = [] outside your readable event handler. You should expect the readable event to fire many times, which means that currently, each time it fires you create a new, empty titles array. That's why you only see the last one.

marti1125 commented 8 years ago

thanks! @danmactough you give me an idea how to solve my problem =D the solution is

  var titles = []
   var chunk;

feedparser.on('readable', function() {
// This is where the action is!
var stream = this
  , meta = this.meta
  , item;

   while (chunk = stream.read()) {

    titles.push(chunk.title)
   }

 });

 feedparser.on('end', function() {
     res.render('index', { title: 'Express', data: titles });
 });
danmactough commented 8 years ago

:tada: