javascript - 将数组从node.js/express传递到jade模板

标签 javascript node.js express pug

不确定我在这里做错了什么..

问题.js

  questions = [];
  questions.AA = 'First'
  questions.BB = 'Second'
  questions.CC = 'Third'
  res.render('questions', { title: questions[CC], 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

似乎没有按照我的预期工作。我缺少什么?

最佳答案

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

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

或者,更惯用的说法是:

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

您必须想出一些办法来替换您获取标题的方式,但这应该可以修复循环。

关于javascript - 将数组从node.js/express传递到jade模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27098251/

相关文章:

node.js - 使用 nodemailer 发送邮件 - 来自字段的电子邮件不正确

javascript - 函数为字符串时如何渲染

javascript - 在kotlin-js中包含js依赖项

javascript - 无法让 JavaScript 代码在 Firefox 上运行

javascript - Node orm2 : orm. 连接回调未调用

javascript - Discord.js();如何发送文件(总是发送0字节文件?)

node.js - Express - 多个角度应用程序

javascript - React JSX 中的“if () {} else {}”语句?

node.js - locomotivejs 和 Mongoose : passing promise variables to a controller

node.js - express.js CSURF cookie 和 header 匹配,返回 403