2011-11-14 72 views
0

server.coffee我:的Node.js:模块不能识别模式

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 
+1

你在做什么来确保'server.coffee'中定义的'User'对'addEntryToCustomer.coffee'是可见的?你将需要一些'module.exports' /'require'。 –

回答

1

在node.js中,模块在自己的上下文中运行。这意味着User变量不存在addEntryToCustomer.coffee

您可以让User全球(小心吧):

global.User = mongoose.model 'User' 

传递用户变量模块:

module.exports = (User, phone, res, req) -> 
    User.find {account_id: phone.account_id }, (err, user) -> … 

或重新加载模型:

mongoose = require 'mongoose' 

module.exports = (phone,res,req) -> 
    User = mongoose.model 'User' 
    User.find {account_id: phone.account_id }, (err, user) -> 

也可以向模型本身添加方法,但在定义时需要这样做Schema:http://mongoosejs.com/docs/methods-statics.html