Mark Hähnel
Mark Hähnel

Reputation: 175

Route data not accessible in template

I have a problem accessing the data from an route.

Router

Router.route('/view/:_id', {
    name: 'locationView',
    data: function() { return Locations.findOne(this.params._id); }
});

Locations is a MongoDB Collection that i've subscribed to. I can access it via a helper method on another template without problems. It has objects like name, address etc.

Template

<template name="locationView">
    <h1>{{name}}</h1>
</template>

Demo

Application hosted on meteor.com

On the front-page you can see that the entries from Locations are accessible from other templates over helper methods. So why i can't access them via the data object from the route?


Solution

The problem was adding the objects via mongo shell. That caused the ObjectID(xxx) stuff to appear in the _id of the object.

I've added some test objects via collection method 'insert' and everything works fine!

Upvotes: 0

Views: 53

Answers (3)

Mark H&#228;hnel
Mark H&#228;hnel

Reputation: 175

Solution

The problem was adding the objects via mongo shell. That caused the ObjectID(xxx) stuff to appear in the _id of the object.

I've added some test objects via collection method 'insert' and everything works fine!

Upvotes: 0

SylvainB
SylvainB

Reputation: 4820

The data defined in your route is stored in your template instance, ergo Template.instance().data. You can also access it in onRendered using simply this.data:

Template.locationView.onRendered(function () {
  var data = this.data;
});

Upvotes: 1

Dennis Stolmeijer
Dennis Stolmeijer

Reputation: 122

I just tried you application and i saw this url:

http://mhaehnel-compass.meteor.com/view/ObjectID(%22554e58c15eb2cd41fc085c0e%22)

The ObjectID() part is just for mongodb and is not supposed to be in you url.. that's why meteor is not doing the thing you expected. This could be an example how you would set your link to the locationView route

{{pathFor 'locationView' _id = this._id}}

Hope this helps

Upvotes: 1

Related Questions