I'm new to meteor and coffeescript. I'm using the file layout suggested in the Unofficial Meteor FAQ. In file collections/C.coffee, I have
C = new Meteor.Collection 'C'
console.log "C: #{C}"
In file server/main.coffee, I have
C.insert {test: 'test'}
When I start meteor, 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 do I make C available in files outside of collections/C.coffee?
Update: Adding @ to C fixes the problem at the top level. However it still fails with:
Meteor.methods
test: (statement) ->
@C.insert {test: 'test'}
It fails with an error TypeError: Cannot call method 'insert' of undefined
To make C visible outside the file it was defined in use @
, which compiles to this.
or window.
in js, which gives it the same effect as a global scope:
@C = new Meteor.Collection 'C'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With