简体   繁体   English

将数组从node.js / express传递到Jade模板

[英]Passing array from node.js/express to jade template

Not sure what I'm doing wrong here.. 不知道我在做什么错..

questions.js questions.js

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

questions.jade questions.jade

extends layout

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

Rendered 渲染

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

So 所以

  each question in questions
   p= question

Doesn't seem to be working as I would expect. 似乎没有按我期望的那样工作。 What am I missing? 我想念什么?

You created an array and then stored values into alphabetic indices rather than integer indices. 您创建了一个数组,然后将值存储到字母索引而不是整数索引中。 As such, each will not loop over them. 这样, each都不会在它们上循环。 You probably mean to define questions like this: 您可能想定义如下questions

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

Or, more idiomatically: 或者,更习惯地说:

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

You'll have to figure something out to replace how you were getting the title , but this should fix the loop. 您必须弄清楚一些事情来替换如何获得title ,但这应该可以解决循环问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM