Jade中的嵌套forEach不起作用

时间:2016-03-29 04:32:24

标签: node.js foreach pug

我将从MongoDB返回的多个员工对象传递给Jade模板。员工对象如下所示:

{ entry_time: 2016-03-26 21:05:27.108Z,
  history: [[room1, 2016-03-26 21:05:27.108Z, 2016-03-26 21:05:27.108Z],
             [room2, 2016-03-26 21:05:27.108Z, 2016-03-26 21:05:27.108Z]],
  current_room: 'meeting03',
  last_name: 'B',
  first_name: 'A',
  eid: '3',
  _id: 56c6592005bb4ebfa29b6526 }

app.js:

app.get('/find', function(req,res){
    return res.render('find', {data: employee});
});

旧的find.jade:

- data.forEach(function(employee){
      - var first_name = employee.first_name
      - var last_name = employee.last_name
      - var current_room = employee.current_room
      - var history = employee.history
      li #{first_name}
      li #{last_name}
      li #{current_room}
      - history.forEach(function(entry) {
               - var room = entry[0]
               - var entry_time = entry[1]
               - var dept_time = entry[2]
      -});
- });

上面的jade代码工作正常,直到我在嵌套的forEach函数中添加了几个li项:

新的find.jade:

- data.forEach(function(employee){
      - var first_name = employee.first_name
      - var last_name = employee.last_name
      - var current_room = employee.current_room
      - var history = employee.history
      li #{first_name}
      li #{last_name}
      li #{current_room}
      - history.forEach(function(entry) {
               - var room = entry[0]
               - var entry_time = entry[1]
               - var dept_time = entry[2]
               li #{room} // I get a warning: missing } 
               li #{entry_time}
               li #{dept_time}
      -}); // another warning: statement expected
- });

我不知道为什么添加三个li元素会破坏嵌套的forEach。知道我应该怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

我将嵌套的forEach更改为:

for entry in history
      - var room = entry[0]
      - var entry_time = entry[1]
      - var dept_time = entry[2]
      li #{room}
      li #{entry_time}
      li #{dept_time}

有效。希望这会帮助别人。