我在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?
答案 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