Ah, yea I should have explained that better. If you have the “import” at the top of the file, then it has to be defined at the point that file is executed. You can wrap the entire file in a Meteor.startup
function and that will solve it. Also you can “import” it inside of the component right before you use it (though this can be time consuming to migrate).
For example:
// in both/models/items.js
const Items = new Mongo.Collection("items");
...
this.Items = Items;
and in your component file:
// in client/components/Items/Items.jsx
const Items = this.Items;
const Component = React.createClass({
getInitialState() {
return {
foo: Items.findOne();
}
}
});
this.Component = Component;
This way it’s not being called until after it’s defined. You could also do this:
// in client/components/Items/Items.jsx
Meteor.startup(function() {
const Items = Items;
const Component = React.createClass({
getInitialState() {
return {
foo: Items.findOne();
}
}
});
Component = Component;
}); // end Meteor.startup
However, I would go a step further and organize your collections into their own modules. Models consume them but they are different things. You could make one collections file like this:
// in both/collections/index.js
const Items = new Mongo.Collection("items");
const Posts = new Mongo.Collection("posts");
this.Collections = {
Items: Items,
Posts: Posts,
};
Then in your files you can do this:
// in client/components/Items/Items.jsx
Meteor.startup(function() {
const {Items} = Collections;
// in 1.3 would be import {items} from 'boths/collections';
const Component = React.createClass({
getInitialState() {
return {
foo: Items.findOne();
}
}
});
Component = Component;
}); // end Meteor.startup
Or since this example is a module (not just a single export) we could do this without the wrapper like this:
// in client/components/Items/Items.jsx
const Component = React.createClass({
getInitialState() {
const {Items} = Collections; // now it's defined at call time
return {
foo: Items.findOne();
}
}
});
Component = Component;
Hope this helps! I need to write this up in a blog post soon