anoop
anoop

Reputation: 539

Uncaught ReferenceError: Collection is not defined

I am doing a meteor application. I have the collection variable defined in the main.js file inside the client folder. There is also templates.js file in the same location. The Template.myTemplate.rendered = function() {}; is defined inside the templates.js file. I can access the collection variable inside the rendered section, but it is not available outside it.

How can it be made possible to access the collection variable outside the 'rendered' section ?

Inside main.js

collectionVariable = new Meteor.Collection('collection_name');

Inside templates.js

console.log(collectionVariable); // getting "Uncaught ReferenceError: collectionVariable is not defined" here.

Template.myTemplate.rendered = function() {

  //Some code. Can access the variable here.

};

Thanks

Upvotes: 4

Views: 7623

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

Keep in mind the file loading order. It appears that templates.js is loaded before main.js in your case, so the collectionVariable is not defined by the time it's evaluated. If you want to be sure you can use a variable in the file body, use Meteor.startup:

var localVariable;

Meteor.startup(function(){
  localVariable = collectionVariable;
  console.log(collectionVariable);
});

Upvotes: 4

Related Questions