Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

javascript - Having problems with passing array to jade template in node.js

I am trying to pass array of news to display on the screen but I somehow am getting empty array in the result in the browser

routes/rss.js

...
var news = [];
...
          var this_news = { 
            'title': item.title,
            'description': item.description
          }   
          news.push(this_news);
...
  res.render('rss', { 
    title: 'Node.js based RSS reader',
    newsi: JSON.stringify(news)
  }); 

views/rss.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  p Sure why not 
  script(type='text/javascript').
    var inews = !{newsi};

EDIT

Ok so I have come to the conclusion that the problem lies with my 'non-callback logic'

that's my code :

var FeedParser = require(__dirname + '/../node_modules/feedparser')
  , request = require(__dirname + '/../node_modules/request');

exports.news = function(req, res){

  var news = []; 
    request('http://feeds.feedburner.com/niebezpiecznik/')
    .pipe(new FeedParser())
      .on('error', function(error) {
        //...
      })  
      .on('meta', function (meta) {
        console.log('===== %s =====', meta.title);
        console.log('**** %s ****', meta.description);
        console.log();
      })  
      .on('readable', function() {
        var stream = this, item;
        while (item = stream.read()) {

          var this_news = { 
            'title': item.title,
            'description': item.description
          }   
          news.push(this_news);

        }   
        res.render('rss', { 
          title: 'Node.js based RSS reader',
          newsi: JSON.stringify(news)
        }); 
    }); 

};

It almost works but there it gets me unhandled exception. How should I handle this ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can just pass it as an array, you don't need to stringify it when you are rendering. Then on your jade side you can just use for & each if you are only wanting to loop through.

To get it to pass as an object to a script though you use an exclamation mark with single quotes then parse your object so you can use it. If you are going to pass it as an object to a script however and not use Jade's built-in iterating then you will need to stringify on your rss.js.

routes/rss.js

...
var news = [];
...
          var this_news = { 
            'title': item.title,
            'description': item.description
          }   
          news.push(this_news);
...
  res.render('rss', { 
    title: 'Node.js based RSS reader',
    newsi: JSON.stringify(news)
  }); 

views/rss.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  p Sure why not
  script(type='text/javascript').
    // Pass as regular array here
    var inews = JSON.parse('!{newsi}');
    console.log(inews);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...