【问题标题】:Passing array from node.js/express to jade template将数组从 node.js/express 传递到玉模板
【发布时间】:2014-11-24 13:20:48
【问题描述】:

不知道我在这里做错了什么..

questions.js

  questions = [];
  questions.AA = 'First'
  questions.BB = 'Second'
  questions.CC = 'Third'
  res.render('questions', { title: questions[CC], questions: questions });

questions.jade

extends layout

block content
  h1= title
  p #{questions.CC}
  each question in questions
   p= question

渲染

<body>
<h1>Third</h1>
<p>Third</p>
</body>

所以

  each question in questions
   p= question

似乎没有像我预期的那样工作。我错过了什么?

【问题讨论】:

    标签: javascript node.js express pug


    【解决方案1】:

    您创建了一个数组,然后将值存储到字母索引而不是整数索引中。因此,each 不会遍历它们。您可能的意思是像这样定义questions

    questions = []
    questions[0] = 'First'
    questions[1] = 'Second'
    questions[2] = 'Third'
    

    或者,更惯用的说法:

    questions = [
        'First',
        'Second',
        'Third'
    ]
    

    您必须想办法替换您获取 title 的方式,但这应该可以解决循环问题。

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 1970-01-01
      • 2014-11-03
      • 2016-11-15
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      相关资源
      最近更新 更多