Saturday, 6 May 2017

Getting Cannot read property 'length' of undefined with Jade and nodeJs

I am trying Node Js with Jade as view from Getting MEAN with Mongo and express and got stuck with one error.

TypeError: C:\Users\Desktop\Loc8r\app_server\views\location-list.jade:16
    14|       .col-xs-12.col-sm-8
    15|         .row.list-group
  > 16|         each location in locations
    17|           .col-xs-12.list-group-item
    18|             h4
    19|               a(href="/location") #{location.name}

Cannot read property 'length' of undefined

I am fetching data from mongo and rendering through jade template .

My Controller is :

var renderHomepage = function(req, res,responseBody){
  console.log(responseBody);  
  res.render('location-list',
  {
      title: 'Loc8r - find a place to work with wifi',
      pageHeader: {
          title: 'Loc8r',
          strapline: 'Find places to work with wifi near you!'
        },
      locations: responseBody
  });

}

When printing console.log(responseBody) I am getting proper json data on

console but it is not being set to locations variable .

When I tried with locations: [] or locations: hardcoded json data , it is working fine

Jade Template is :

extends layout

include ../_includes/sharedHTMLfunctions
block content
    h1= title
    p Welcome to #{title}

    #banner.page-header
        .row
            .col-lg-6
                h1= pageHeader.title
                    small  #{pageHeader.strapline}
    .row
      .col-xs-12.col-sm-8
        .row.list-group
        each location in locations
          .col-xs-12.list-group-item
            h4
              a(href="/location") #{location.name}
              small  
              +outputRating(location.rating)
              span.badge.pull-right.badge-default= location.distance
            p= location.address
            p
              each facility in location.facilities
               span.label.label-warning= facility
                



      .col-xs-12.col-sm-4
        p.lead Looking for wifi and a seat? Loc8r helps you find places to work when out and about. Perhaps with coffee, cake or a pint? Let Loc8r help you find the place you're looking for.

responseBody is printing following on console

[ { distance: 0,
    name: 'Starcups',
    address: '125 High Street, Reading, RG6 1PS',
    rating: 3,
    facilities: [ 'Hot drinks', 'Food', 'Premium wifi' ],
    _id: '58f99a571fdade0bdfddce4b' }
]



via Priti Guleria

No comments:

Post a Comment