Jebathon
Jebathon

Reputation: 4561

Meteor, cannot retrieve document data from a collection to the client within a template

I am having difficulty displaying items from the database to the client within Meteor. Within the template user_places I want to display the contents of a document from the collection and sort each place by the creation of the document.

However although the code compiles and runs I see nothing displayed where {{>user_places{{ is located.

I suspect I am putting the wrong query in:

return Places_Collection.find({ownerId: Meteor.userId()}, {sort: {addedAt: -1}});

Anyway here is my code. Note Meteor.userId() does contain a value so lets assume there is a user logged in.

html:

<head>
<title>Test</title>
<head>

<body>
{{ul}}    
{{#each places}}
{{>user_places}}
{{/each}}
{{/ul}}    
...
</body>

<template name="user_places">
<li>You have been to {{place_id}} from {{start}} to {{end}}</li>
</template>

javascript:

...
//client code
Meteor.subscribe('places_collection');

Template.user_places.helpers({

 places: function() {
return Places_Collection.find({ownerId: Meteor.userId()}, {sort: {addedAt: -1}});
}
)};
 ...

Sample document within Places_Collection (Mongodb)

{
added_At: ISODate("2015-0207TIF:39:48:837Z"), 
ownerId: "~userToken~",
start: "2014-02-02",
end: "2014-04-03:,
place_id "Canada"
_id: "~documentToken~"
}

If you need any more code don't hesitate to ask.

Upvotes: 0

Views: 75

Answers (1)

xamfoo
xamfoo

Reputation: 151

Try defining places helper in Template.body instead since it is within <body></body> instead of a template definition.

Template.body.helpers({
  places: function() {
    return Places_Collection.find({ownerId: 'asdf'}, {sort: {addedAt: -1}});
  }
});

Here is a working version: http://meteorpad.com/pad/uk8LzZPGs4nmjuhQ6/Example%201

Upvotes: 1

Related Questions