1

我遇到了最近的問題,我的路由器似乎沒有收集路由名稱。路由器收不到收集對象路由到Colleciton文檔

我有一個名爲Nodes的集合。在這個集合中,從每個節點中的各種數據的許多節點有很多讀取。雖然這些節點的名稱屬性不唯一(使用simpleSchema)。這是特定的節點可以發送多點數據。稍後我將從集合中繪製這些數據。 節點插入是

var sampleInput_4 ={ 
    name : "Tulsa Node", 
    longitude : -95.982, 
    latitude : 36.137, 
    humidity : 78, 
    tem p: 80, 
    dew : 20, 
    pressure : 32, 
    speed : 12, 
    direction : 60 
}; 

但也可能有數以千計的插件。數千個不同的節點插入。我發佈整個節點集合,並在側邊欄js文件中訂閱創建到整個集合。這只是爲了測試這個問題。這裏是側邊欄的js文件。

Template.sidebar.helpers({ 
    nodes: function(){ 
     var col= Nodes.find().fetch(); 
     return _.uniq(_.pluck(col,'name')); 
    } 
}); 

Template.sidebar.onCreated(function() { 
    this.subscribe('nodes'); 
}); 

這工作正常在HTML加載只是像我想要的唯一名稱。

{{#each nodes}} 
    <li> 
     <a href="{{pathFor 'NodePage'}}"> 
      {{this}} 
     </a> 
    </li> 
{{/each}} 

但是,這並不是我想要的方式。當我這樣做時,實際上沒有路線。我想讓路線成爲唯一名稱的名稱。哪個文件的哪個名稱無關緊要只要是我點擊的唯一名稱即可。 這裏是路由器

Router.route('/:_id', { 
name : 'NodePage', 
data : function() { return Nodes.findOne(
     // this refers to the currently matched 
     //route.this.params access parts of route 
     this.params._id); 
    } 
}); 

雖然如果我把

return Nodes.find(); 

在側邊欄的js文件退回路線的作品。我是否缺少鐵路路由器的一些基本方面?此後的側邊欄只返回整個集合中的每個[對象]。儘管您可以點擊這些,路由器也可以使用它們。

回答

1

原來,路由器使用它需要的名稱屬性來將它從對象中拉出來,所以我通過HTML中的每個代碼發送了一個對象數組。所以幫助者只需要形成具有唯一名稱的對象數組即可返回。

Template.sidebar.helpers({ 
    nodes : function() { 
     //make array to hold objects 
     var myObjectArray = []; 
     // grab entire collection 
     var nodeCollection = Nodes.find().fetch(); 
     // Get the unique names from collection 
     var nodeNames = _.uniq(_.pluck(nodeCollection,'name')); 
     // find the Node with that name and 
     // place into object array loop till done 
     for(i = nodeNames.length; i>0; i--){ 
      var arrayItem = nodeNames[i-1]; 
      var nodeObject = Nodes.findOne({name: arrayItem}); 
      myObjectArray.push(nodeObject); 
     } 
     return myObjectArray; 
    } 
});