我在MEAN堆栈上完成了Lynda.com课程,这个错误不断发生。我不确定在哪里看。我已经浏览了许多Google页面,寻找可能给我一些线索的任何内容
这是我调用findUser的地方。
module.exports = function() {
var passport = require('passport');
var passportLocal = require('passport-local');
var userService = require('../services/user-service');
passport.use(new passportLocal.Strategy({usernameField: 'email'}, function(email, password, next) {
userService.findUser(email, function(err, user) {
if (err) {
return next(err);
}
if (!user || user.password !== password) {
return next(null, null);
}
next(null, user);
});
}));
passport.serializeUser(function(user, next) {
next(null, user.email);
});
passport.deserializeUser(function(email, next) {
userService.findUser(email, function(err, user) {
next(err, user);
});
});
};
这是我在尝试登录时遇到的错误"在数据库中验证用户信息。
TypeError: Object #<Object> has no method 'findUser'
at Strategy.module.exports [as _verify] (/home/ubuntu/workspace/auth/passport-config.js:7:17)
at Strategy.authenticate (/home/ubuntu/workspace/node_modules/passport-local/lib/strategy.js:90:12)
at attempt (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:341:16)
at authenticate (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:342:7)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:82:5)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:82:5)
at proto.handle.c (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:267:22)
at Function.proto.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:321:12)
这是user-services.js文件
var User = require('../models/user').User;
exports.addUser = function(user, next) {
var newUser = new User({
firstName: user.firstName,
lastName: user.lastName,
roomNumber: user.roomNumber,
email: user.email,
password: user.password
});
newUser.save(function(err) {
if (err) {
return next(err);
}
next(null);
});
};
答案 0 :(得分:0)
您的userService导出对象没有findUser方法。
如果原始函数的名称为module.exports.findUser = findUser;
,则通过添加findUser
来导出它。如果您已经导出了像:
module.exports = {
foo: foo
};
只需将findUser: findUser
添加到该对象。