因此,尝试创建一个简单的模块来整理网页的帐户,并认为这很简单。创建一个包含一些函数的文件,将它们全部包装在module.exports中,然后让你的叔叔陷入困境。好吧,结果不是我的叔叔。
因此创建了以下文件:
module.exports = function(lameAccount) {
lameAccount.initAccount( function ( myId, myName, done) {
console.log("Will create" + myId);
console.log("Name is " + myName);
done(null,false,"This is just a hello");
})
}
在需要它的部分(api路由器)
var lameAccountant = require('../modules/lameaccount.js')
然后调用我们的函数
lameAccount.initAccount(blockRecord._id, blockRecord.betName)
然而,当我尝试使用此功能时,我得到了
/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:108
if (this.ended && !this.hasRejectListeners()) throw reason;
^
TypeError: undefined is not a function
at EventEmitter.<anonymous> (/Users/bengtbjorkberg/WebstormProjects/Challange/routes/api.js:119:22)
at EventEmitter.<anonymous> (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45)
at EventEmitter.emit (events.js:110:17)
at Promise.safeEmit (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
at Promise.fulfill (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24)
at Promise.resolve (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/lib/promise.js:113:23)
at model.<anonymous> (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/lib/document.js:1578:39)
at next_ (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:89:34)
at EventEmitter.fnWrapper (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:186:8)
at EventEmitter.<anonymous> (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45)
at EventEmitter.emit (events.js:110:17)
at Promise.safeEmit (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
at Promise.fulfill (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24)
at p1.then.then.self.isNew (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/lib/model.js:260:27)
at newTickHandler (/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:229:18)
at process._tickCallback (node.js:355:11)
==============编辑第一个答案============
所以,如果这是一个匿名函数(确定它,为什么这样做)
// expose this function to our app using module.exports
module.exports = function(passport) {
// =========================================================================
// passport session setup ==================================================
// =========================================================================
// required for persistent login sessions
// passport needs ability to serialize and unserialize users out of session
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
或者确实是apis本身
module.exports = function(app, passport) {
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// parse application/json
app.use(bodyParser.json())
app.get('/api/userList', function(req, res){
User.find({}, {'_id':1, 'userName':1},function(err, users) {
if (err)
res.send(err)
res.json(users);
})
});
这就是app.use所做的,允许你在匿名函数中滑动。
答案 0 :(得分:0)
您将匿名函数附加到module.exports
,因此initAccount
为undefined
。
使用匿名函数:
var lameAccountantModule = require('../modules/lameaccount.js')
// call the function like you would the anonymous function
lameAccountModule(lameAccount)
或者公开你想要的那个,initAccount
,而不是:
exports.initAccount = function ( myId, myName, done) {
console.log("Will create" + myId);
console.log("Name is " + myName);
done(null,false,"This is just a hello");
}
然后在你的另一个文件中:
var lameAccount = require('../modules/lameaccount.js')
lameAccount.initAccount(myId, myName, callback)