get length of embedded document in mongoDB with jade

53 Views Asked by At

i have a simple messageboard with comments. i want to show the amount of comments on the index page. the comments for each post are embedded in the thread document.

i thought it would be pretty easy, but it doesn't seem to work.

thread data is like

"authorName": String,
"postedAt": Date(),
"bumpedAt": Date(),
[...]
"comments": [{
    "author": String,
    "comment": String
]}

(tried with and without square brackets... not really sure which one is correct)

i use jade and load the whole database for the index page

collection.find( { $query: {}, $orderby: { "bumpedAt": -1 } },{}, function(e,docs) {
    res.render('index', { title: 'Index', posts: docs });
})

now i want to show how many comments there are for every thread.

each post, i in posts
[...]
  span#commentCount (#{post.comments.length} || (0))

doesn't work..

Cannot read property 'length' of undefined

can someone tell me why? thanks

1

There are 1 best solutions below

0
On

question can be closed, i tried around and found the problem.

had to initiate each thread with

"comments":[]

and then it worked. so easy yet so hard to find out...