Access nested objects property with pug

3.6k Views Asked by At

I have this object called obj in my pug template

{
  "property1": {
    "property1": {
      "property1": "value",
      "property2": "value",
      "property3": "value"
    },
    "property2": "value",
    "property3": "value"
  },
  "property2": "value",
  "property3": "value"
}

If i do

pre=obj

The object is displayed correctly. But if I want to access its nested properties like

pre=obj.property1.property1

For some reasons its not rendering.

Same if I do p=obj.property1.property1.property1

What am I missing? Thanks

Here is my exact code snippet:

  .box-body
    -session_details.forEach(function(session_detail) {
      .row
        .col-md-1=moment(session_detail.create_time).calendar()
        .col-md-1=moment(session_detail.create_time).format("DD/MM/YYYY HH:MM:SS")
        .col-md-1=session_detail.intent
        .col-md-4
          p #{session_detail.request.type}
          pre=session_detail.request

        .col-md-5
          pre=session_detail.response
    - });

And here is the exact output: https://i.stack.imgur.com/tx1Tt.jpg

As you can see, the p #{session_detail.request.type} is not displayed for some reasons.

2

There are 2 best solutions below

0
On

If you are using obj.find() instead of obj.findOne() to get your instance of the "obj" model, then an array with one object ([obj]) will be returned, instead of just the obj itself. If that's the case, you won't be able to access the properties of it without accessing the index in the array first (obj[0].property1).

4
On

It's been asked already but I don't have enough reputation to flag as duplicate.

To answer your question, use hash and curly braces.

p #{property1.property1}

I don't think you need the equal sign though.