2015-02-09 50 views
0

訪問數據我有一個課程集合與路線

{courses{{course1_title,id,author,pages[{title,content,model, etc...}, 
       [ etc...]{course2,....}} 

我試圖通過路由器顯示的課程集合內的當前頁面的數據

this.route('page', { 
    path: '/:title', 
    data: function() { 
    return courses.findOne({'pages.title':this.params.title}; 

    } 

}); 

我想顯示當前像這樣的頁面的數據:

<template name="page"> 
<div class ="container page"> 
    <h1>Page</h1> 
    <!--display current page data --> 
    <h3>{{title}}</h3> 
    <div>{{ an other content}} etc..... </div> 
</div> 

現在,路由器返回次整個課程的數據和顯示的標題都是課程的標題。我找不到如何訪問當前頁面的數據,以便將其顯示在頁面的模板中。

我試圖

return courses.findOne({'pages.title':this.params.title}{fields:{{'pages.title':this.params.title}:1}} 

和很多其他的方式。我沒有找到它。

什麼是正確的方法?

回答

0

您當前的查詢會搜索所有的課程匹配的網頁標題,然後返回整個過程(所有頁面)

你應該回到問題僅在該頁面中的數據:

course = courses.findOne({pages: { $elemMatch: { title: this.params.title }}); 
return course.pages[0] 

另外,最好爲頁面創建一個單獨的集合(將每個頁面鏈接回課程ID)。儘管這不是「Mongo」,但Meteor只能對收集而不是子文檔進行反應式操作。

+0

最後,我決定遵循你的建議。非常感謝,MK – michelk 2015-02-10 16:11:33