路由器没有收到Collection对象以路由到Collection文档

时间:2016-02-26 18:00:35

标签: meteor collections iron-router meteor-collection2 meteor-collection-hooks

我遇到了一个最近的问题,我的路由器似乎没有集合来路由这个名字。

我有一个名为Nodes的集合。在这个集合中,有许多节点从每个节点中读取各种数据。虽然这些节点的name属性不是唯一的(使用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文件中返回路由工作。我错过了Iron路由器的一些基本方面吗?此后的侧边栏只返回整个集合中的每个[对象]。虽然您可以单击这些,但路由器可以使用它们。

1 个答案:

答案 0 :(得分:1)

结果是路由器使用从一个对象拉出它所需的name属性,所以我通过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;
    }
});