Rhys Edwards Rhys Edwards - 7 days ago 5
Jade Question

Return all subdocs to Jade template

I have a document in MongoDB that includes subdocuments,

destinations
as a field. I want to return this data back to my Jade template, however, this doesn't seem to be working.

User document:

{ _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 }


Jade template

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


How do I call the subdocs and iterate through it's results to render into html?

Answer

You can use 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
Comments