【问题标题】:Router doesn't receive Collection object to route to the Collection document路由器没有接收到 Collection 对象以路由到 Collection 文档
【发布时间】:2016-06-10 01:49:50
【问题描述】:

我最近遇到了一个问题,我的路由器似乎没有用于路由名称的集合。

我有一个名为 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 文件中订阅了 Created 到整个集合。这只是为了测试这个问题。这是侧边栏的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}}

但是,这并没有按照我想要的方式路由。当我这样做时,实际上没有路线。我希望路线是 /name 的唯一名称。哪个文件的哪个名称无关紧要。只要它是我点击的那个,它就是任何唯一的名称。 这是路由器

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 路由器的一些基本方面吗?此后的侧边栏也只返回整个集合中的每个 [object]。虽然你可以点击这些,路由器就会对它们起作用。

【问题讨论】:

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


    【解决方案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;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-27
      • 2019-04-10
      • 2013-09-12
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2020-07-26
      相关资源
      最近更新 更多