Router doesn't receive Collection object to route to the Collection document

47 Views Asked by At

I have ran into a recent problem where my router doesn't seem to have the collection to route the name.

I have a collection named Nodes. In this collection there are many reading from many nodes with various kinds of data in each node. Although the name attribute for these nodes are not unique(using a simpleSchema). This is that particular node can send many points of data. Later I will graph this data from the collection. Nodes insert is

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
};

But there could be thousands of these inserts. And thousands of a different nodes inserts. I publish the whole nodes collection and in the sidebar js file is subscribe on Created to the whole collection. this is just for testing this problem. here is the sidebar js file.

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

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

This works fine in the HTML loading just the unique names like I want.

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

However this does not route the way I want. There in fact is no route when I do it this way. I want the route to be /name of the unique name. Does not matter which name of which document. just the unique name of any as long as it is the one I clicked on. Here is the router

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);
    }
});

Although If I put

return Nodes.find();

in the sidebar js file for the return the route works. Am I missing some fundamental aspect of Iron router? Also the sidebar after this just returns every [object] in the entire collection. Although you can click on these and the router works on them.

1

There are 1 best solutions below

0
On

Turns out for the Router to use the name attribute it needed to be pulling it from an object so I sent an array of object through the each code in the HTML. So the helper just needed to form an array of object with unique names to return.

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;
    }
});