有问题,传球达阵玉模板的node.js [英] Having problems with passing array to jade template in node.js

查看:212
本文介绍了有问题,传球达阵玉模板的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

路线/ 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)
  }); 

的意见/ rss.jade

extends layout

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

修改

好了,所以我来了,问题在于我的'非回调逻辑

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

这是我的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 ?

推荐答案

您可以只把它当作一个数组,你不需要它字符串化当你呈现。然后在你的身边玉你可以使用为&放大器;每个如果你只想要通过循环。

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.

要得到它的传递为对象,以虽然你使用一个感叹号单引号然后解析你的对象,所以你可以使用它脚本。如果你打算然而,把它作为一个对象的脚本,而不是用玉的内置迭代,然后你将需要字符串化你的 rss.js

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.

路线/ 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)
  }); 

的意见/ 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);

这篇关于有问题,传球达阵玉模板的node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆