Reputation: 766
So I am trying to return a mongoDB database value to a template in my Meteor.js project. The code that I am using is below.
Template.ResourceManager.helpers({
BoosterOneFuel : function(){
return resources.findOne({system : "booster1"}).fuel;
}
});
However, this always returns null. When I try to alert it, the alert also says that this value is null. Mongo can find it when I run this line in the console while running meteor mongo:
db.Resources.findOne({system : "booster1"}).fuel;
But meteor cannot. (This is on a localhost, so meteor mongo should affect meteor's database)
I don't think its a problem with meteor loading before mongo does, because the following still doesn't work :
if(resource.find({system : "booster1"}))
alert(resources.findOne({system : "booster1"}).fuel);
Anybody know whats going on here? Thanks in advance.
Upvotes: 1
Views: 825
Reputation: 64312
Assuming the collection is actually called resources
- i.e. you have something that looks like:
resources = new Mongo.Collection('Resources');
Then it sounds like you just need to publish the documents to the client:
server/publishers.js
Meteor.publish('resources', function() {
return resources.find();
});
client/subscriptions.js
Meteor.subscribe('resources');
Of course the subscription could happen in your template or router instead of globally, but that's beyond the scope of this question.
Also note you should add a guard to your helper. For example:
Template.ResourceManager.helpers({
BoosterOneFuel : function() {
var b1 = resources.findOne({system : "booster1"});
return b1 && b1.fuel;
}
});
Upvotes: 4