javascript 在 node.js 中将数组传递给 jade 模板时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20294541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:09:45  来源:igfitidea点击:

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

javascriptarraysnode.jspug

提问by Patryk

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

路线/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

意见/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 ?

它几乎可以工作,但它让我遇到了未处理的异常。我该如何处理?

回答by Andrew Lively

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 & eachif you are only wanting to loop through.

您可以将其作为数组传递,渲染时无需对其进行字符串化。然后在你的翡翠方面如果你只想循环,你可以只使用for & each

To get it to pass as an object to a scriptthough 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.

为了让它作为对象传递给 a ,script尽管您使用带单引号的感叹号然后解析您的对象以便您可以使用它。但是,如果您要将它作为对象传递给脚本而不使用 Jade 的内置迭代,那么您将需要在rss.js.

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

views/rss.jade

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