0

I want to render this data on my view with Jade. This is returned from the QPX api. I have data structured like this.

{ 
  kind: 'qpxExpress#tripsSearch',
  trips: { 
    kind: 'qpxexpress#tripOptions',
    requestId: 'OqaX52F0Z433IbMlh0PNr6',
    data: {
      kind: 'qpxexpress#data',
      airport: [Object],
      city: [Object],
      aircraft: [Object],
      tax: [Object],
      carrier: [Object]
    },
    tripOption: [ [Object], [Object], [Object] ] 
  }
}

How do I access the nested object array in tripOption using Jade? I can't seem to work it out. This is my Jade template:

block content
  .ui
    for data in result
      .ui_box
        .ui_box__inner
          .event
            span #{data.tripOption[0].slice[0]}

This is the error I receive from Jade

Cannot read property '0' of undefined

Router

router.get('/', function(req, res, next) {
  api.apiGet(function (data) {
    console.log(data) 
    res.render('index', {result: data})
  })
})
8
  • how does your route handler look like when passing the data to your view? There is a disconnect between the data and the view atm without that knowledge. Commented Nov 4, 2016 at 12:01
  • I have updated the question with the route Commented Nov 4, 2016 at 12:04
  • #{data.tripOption[0].slice[0]} the [ ] after slice bothers me... Commented Nov 4, 2016 at 12:07
  • That's from the API documentation but I have tried to remove it already and I still receive the error. Commented Nov 4, 2016 at 12:08
  • .slice() is an array method on javascript not jade. anyway, have you tried #{data.trips.tripOption[0]}? notice the trips between data and tripOption. Commented Nov 4, 2016 at 12:10

1 Answer 1

1

Your for loop is iterating on an object, which won't work. This isn't the usual for..in loop in native javascript

The code below modifies the router a little and passes the data (tripOptions) to the view in a more direct way. First the Jade template:

block content
  .ui
    for data in tripOptions
      .ui_box
        .ui_box__inner
          .event
            span #{data.someProp}

new router with the tripOption data made available to the view as tripOptions (plural):

router.get('/', function(req, res, next) {
  api.apiGet(function (data) {
    console.log(data) 
    res.render('index', {
      tripOptions: data.trips.tripOption
    })
  })
})
Sign up to request clarification or add additional context in comments.

3 Comments

Okay this is great but now I'm being thrown another error: Cannot read property 'length' of undefined. It's on this line for data in tripOptions
yeah seems like tripOptions isn't pointing to an array. Maybe console.log(data) before you call res.render and see if you can access the data sowly, e.g. trying console.log(data.trips) then drilling deeper one property at a time.
Thanks, got there!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.