Node.js:模块无法识别架构
在 server.coffee
上,我有:
User = mongoose.model 'User', s.UserSchema
addEntryToCustomer = require './lib/addEntryToCustomer'
在 addEntryToCustomer.coffee
上,我有:
module.exports = (phone,res,req) ->
User.find {account_id: phone.account_id }, (err, user) ->
我收到此错误:
2011-11-14T19:51:44+00:00 app[web.1]: ReferenceError: User is not defined
On server.coffee
I have:
User = mongoose.model 'User', s.UserSchema
addEntryToCustomer = require './lib/addEntryToCustomer'
and on addEntryToCustomer.coffee
I have:
module.exports = (phone,res,req) ->
User.find {account_id: phone.account_id }, (err, user) ->
And I get this error:
2011-11-14T19:51:44+00:00 app[web.1]: ReferenceError: User is not defined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Node.js 中,模块在自己的上下文中运行。这意味着
User
变量在 addEntryToCustomer.coffee 中不存在。您可以将
User
设为全局(小心):将用户变量传递给模块:
或者重新加载模型:
也可以向模型本身添加方法,尽管您需要在以下情况下这样做:定义架构: http://mongoosejs.com/docs/methods-statics.html
In node.js, modules run in their own context. That means the
User
variable doesn't exist in addEntryToCustomer.coffee.You can either make
User
global (careful with it):Pass the user variable to the module:
Or reload the model:
It's also possible to add methods to the Models themselves, though you need to do that when defining the Schema: http://mongoosejs.com/docs/methods-statics.html