我是 meteor 和 CoffeeScript 的新手。我正在使用非官方 Meteor 常见问题解答中建议的文件布局。在文件集合/C.coffee 中,我有
C = new Meteor.Collection 'C'
console.log "C: #{C}"
在文件 server/main.coffee 中,我有
C.insert {test: 'test'}
当我启动 meteor 时,我在控制台上看到:
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
如何使 C 在 collections/C.coffee 之外的文件中可用?
更新:在 C 中添加 @ 可以解决顶层问题。但是它仍然失败:
Meteor.methods
test: (statement) ->
@C.insert {test: 'test'}
它失败并出现错误
TypeError: Cannot call method 'insert' of undefined
最佳答案
为了使 C 在文件之外可见,它在使用中定义 @
,编译为 this.
或 window.
在 js 中,这使它具有与全局范围相同的效果:
@C = new Meteor.Collection 'C'
关于coffeescript - 在 meteor 0.6.4.1/coffeescript 中,可变可见性如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18150532/