Jade Cannot read length of undefined property in every loop

I have this code in my controller that fetches an object from mongo and sends it to the client:

 index: function(req, res){
    List.find({user:req.session.user.id}).exec(function foundLists(error, foundLists) {
        if(error) {
            return res.json({error:error});
        } else {
            return res.view({ title:'Lists',lists:foundLists });
        }
    });
 }

      

In my opinion I am doing the following:

extends ../layout
  block content
    .container
        p #{lists}

      

What is he doing: [object Object],[object Object]

If i do p= JSON.stringify(lists)

It displays:

[{"user":"546109c0d640523d1b838a32","name":"third","createdAt":"2014-11-11T19:39:36.966Z","updatedAt":"2014-11-11T19:39:36.966Z","id":"546265f83e856b642e3b3fed"},{"user":"546109c0d640523d1b838a32","name":"forth","createdAt":"2014-11-11T19:42:09.268Z","updatedAt":"2014-11-11T19:42:09.268Z","id":"546266913e856b642e3b3fef"}]

      

I am trying to achieve:

#lists
    each list in lists
       p #{list}

      

But I am getting this error: Cannot read property 'length' of undefined

I am using Sails and Jade 1.7.0

+5


source to share


2 answers


You have an array of objects, so if you do each list in lists

, then list

is an object. I am assuming Jade wants the string. If you put p #{list.name}

or something like that, that should work.

If you want to show everything you can try, insert your loops like



each list in lists
    each item in list
        p #{item}

      

0


source


This error can occur when at least one of the elements in the lists does not have a property list

. You can protect this with an if statement:



each val in lists
    if val.list
        each item in val
            p #{item}

      

0


source







All Articles