user139014
user139014

Reputation: 1495

uncaught reference error in meteorjs

I'm trying to build an app with meteor.js. In the lib directory, I've got a file collections.js that looks like:

var Datum = new Meteor.Collection('datum');

if (Meteor.isServer && Datum.find().count() == 0) {

var datum = [{...}]

    _.each(datum, function(data) {
  Datum.insert(data);
});
}    

And then in my .js file in the client directory looks like:

Template.datum.helpers({
  datum: function() {return Datum.find(); }
}); 

When I try to run the app, I get Uncaught ReferenceError: Datum is not defined and a blank page. I can't for the life of me figure out why that's happening. Any suggestions or help? I'm sure it must me be doing something stupid but I just can't figure out what it is.

Upvotes: 1

Views: 371

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

Remove the var keyword.

 


 

All javascript files in Meteor are embedded in the (function(){...})() pattern, therefore all variables defined with the var keyword are local to the file. If you want to create a global variable, visible to the whole application, do it without the keyword.

So, instead of

var Datum = new Meteor.Collection('datum'); // local

there should be

Datum = new Meteor.Collection('datum'); // global

Upvotes: 4

Related Questions