Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade Cannot read property 'length' of undefined in each loop

Tags:

node.js

pug

I have this code in my controller retrieving an object from mongo and sending 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 view I do the following:

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

Which renders:[object Object],[object Object]

If I do p= JSON.stringify(lists)

It renders:

[{"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'm trying to achieve:

#lists
    each list in lists
       p #{list}

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

I'm using Sails and Jade 1.7.0

like image 770
tomasso Avatar asked Nov 12 '14 10:11

tomasso


1 Answers

You have an array of objects, so if you do each list in lists then list is an object. I assume Jade wants a string. If you put p #{list.name} or something similar that should work.

If you want to show everything you can try nesting your loops like

each list in lists
    each item in list
        p #{item}
like image 133
dlsso Avatar answered Sep 18 '22 18:09

dlsso