Collection is not defined, shows up in mongol, iron:router problem?

I have a collection im trying to do a basic insert on, but am getting a Uncaught ReferenceError: teams is not defined. I have declared the collection in a common file, and have autopublish on. I can also see the collection using msavin:mongol.

Im guessing I need to do something with the route and iron router but im not sure what. Ideas?

Well, the error message just literally means that your collection variable is not defined, so it has nothing to do with publishing the collection’s items or not. Either you have a file load order issue, typo or visibility issue (var teams = ... instead of teams = new Mongo.Collection(...)).

1 Like

@seeekr Im using coffeescript so using var isnt needed, and im pretty sure theres no typo lol. I dont think its a load order issue, because i can see the collection in mongol, and the insert is being called on a click event so its not like load timing is an issue. Is there anything that needs to be declared in the route to use collections?

Look at the compiled output of the Coffeescript. And in another file then check the visibility of the collection variable, i.e. teams. You’re ignoring the specifics of the JS error message at your own peril here, because it clearly states that the variable you’re trying to access is not defined.
Here’s something from the CS reference for you:

If you’d like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS.

(Implying: Otherwise they’ll be just local variables not visible outside the current script file!)

1 Like

So adding the insert statement right underneath where i created the collection works. Its odd that this is a scope problem as ive yet to see any tuts or examples that have had issues with this. Do you know of any example code on how to properly create the collections so they are visible?

Do it like this:

@teams = new Mongo.Collection "teams"

Since this in the global context means window or the global scope, depending on the environment, the equivalent of this.teams = ... accomplishes what you need. I vaguely remember seen it done like this somewhere in Meteor code, but not entirely sure. Haven’t worked with CS too much.

1 Like

edit: @seeekr ninjad lol

update: ok so declaring the collection with @collection seems to work. Now i noticed this statement in the docs:
If you want to share variables between .coffee files in the same package, and don't want to separately declare them in a .js file, we have an experimental feature that you may like. An object called share is visible in CoffeeScript code and is shared across all .coffee files in the same package. So, you can write for a value that is shared between all CoffeeScript code in a package, but doesn't escape that package.

Seems like using share.collection should work, but it doesnt. Does anyone have any experience with this feature?

And again – you’re not doing yourself a favor if you say (and think!) something like

since the error message clearly indicates that. Whatever else is going on the first thing an effective developer has to do is pay attention to what is happening precisely, and that includes the precise wording of error messages.

Emphasis: "same package"
Are you really working within a package, and not in top-level app code?

Your first post read like you were suggesting the collection itself wasnt being declared, which i knew was false as mongol was showing it.

No not a package. Thanks for the info.