It seems like the data may not always be available when you make the find
call (you can try using findOne
instead).
Based on your comments, it sounds like you might want to place the function calling find
inside a Tracker.autorun()
so that the variable updates as new data is received. If this code isn't within a template helper, it won't be re-run as new data arrives from the server. If something in your template relies on it, then it may be best to include it in a helper within your template.
If the data will not change after the server has started up, you can utilize publish and subscribe mechanisms to declare your data as ready()
once it's loaded on the server. You can then perform client-side processing in an onReady()
callback within your subscribe()
function, like this:
On the server:
Meteor.publish("startupData", function (){
if (!this.userId)
return this.ready();
else
return StartUpData.find({userId: this.userId});
}
On the client:
var startupData;
Meteor.subscribe("startupData", function(){
var initStartupData = StartUpData.find({}) //will retrieve all published data for this user
startupData = doPostProcessing(initStartupData)
}
Furthermore, in the case of unchanging initial data, ensure that any template dependent on the data doesn't render until the data has been received. I've managed this scenario before using IronRouter's waiton
feature, such as:
Router.route('/path', {
name: "waiting_route_name",
waitOn: function(){
return Meteor.subscribe('yoursub', function post_process_onready(){
//post processing here
});
},
loadingTemplate: 'loading',
action: function(){
this.render("the_template_name")
}
})
That being said... If there's a template relying on specific data, it's often a sign that the data should either be provided in a helper for that template or as the data context itself. Meteor will handle re-rendering automatically as the data arrives or changes.