将所有子块返回到Jade模板

时间:2016-11-24 16:46:41

标签: javascript node.js mongoose pug

我在MongoDB中有一个文档,其中包含子文档destinations作为字段。我想将这些数据返回给我的Jade模板,但是,这似乎没有用。

用户文档:

{ _id: 582c7a1c60cd0447daa64b5e,
  firstName: 'a',
  lastName: 'a',
  username: 'a@a.com',
  homeAirport: 'syd',
  __v: 7,
  destinations: 
   [ { airport: 'rhys', month: 'rhys', _id: 5837165c87373103f13921f9 },
     { airport: 'bob', month: 'bob', _id: 583716ae79f2e80498aa33ef },
     { airport: 'check',
       month: 'ceck',
       _id: 583716d4ba757104b0fcc8dd }

玉模板

if (user)
  include addDestinations.jade
  p The email address is #{user.username}
  p My first name is #{user.firstName}
  p My last name is #{user.lastName}
  p I fly from #{user.homeAirport}
  p I wanna go to #{user.destinations.aiport}
  a(href='/logout') Log out
  div
    - console.log(user)
  div

如何调用subdocs并迭代它的结果以呈现为html?

1 个答案:

答案 0 :(得分:1)

您可以使用jade iteration

if (user)
  include addDestinations.jade
  p The email address is #{user.username}
  p My first name is #{user.firstName}
  p My last name is #{user.lastName}
  p I fly from #{user.homeAirport}
  p
   each dest in user.destinations
    // Must use =
    p= dest.airport
  a(href='/logout') Log out
  div
    - console.log(user)
  div