2015-06-08 30 views
0

我有一個簡單的留言板與評論。 我想顯示索引頁面上的評論數量。 每條帖子的評論都嵌入到線索文檔中。用mongoDB獲取嵌入文檔的長度與玉

我認爲這很容易,但它似乎並沒有工作。

線程的數據就像

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

(試圖與不方括號......真的不知道哪一個是正確的)

我用玉和加載整個數據庫

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

現在我想顯示每個線程有多少評論。

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

不工作..

Cannot read property 'length' of undefined 

有人可以告訴我爲什麼? 謝謝

回答

0

問題可以關閉,我試了一下,發現問題。

不得不啓動每個線程有

"comments":[] 

,然後它的工作。 這麼容易,但很難找出...