In meteor 0.6.4.1/coffeescript, how does variable visibility work?

I am new to meteor and coffee houses. I use the file layout suggested in the Unofficial Meteor FAQ. In the collections of /C.coffee files I have

C = new Meteor.Collection 'C' console.log "C: #{C}" 

In the file server /main.coffee I have

 C.insert {test: 'test'} 

When I launch a meteorite, I see on the console:

 C: [object Object] ReferenceError: C is not defined at app/server/main.coffee.js:5:1 at /home/xxx/yyy/.meteor/local/build/server/server.js:298:12 

How to make C available in files outside of /C.coffee collections?

Update: adding @ to C fixes the problem at the top level. However, it still does not work:

  Meteor.methods test: (statement) -> @C.insert {test: 'test'} 

Error with error TypeError: Cannot call method 'insert' of undefined

+8
coffeescript meteor
source share
1 answer

To make C visible outside the file, it was defined using @ , which compiles into this. or window. in js, which gives it the same effect as the global scope:

 @C = new Meteor.Collection 'C' 
+13
source share

All Articles