对象 #<Object> 没有方法 'findUser'
Object #<Object> has no method 'findUser'
我完成了关于 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);
});
});
};
这是我尝试 "login" 在数据库中验证用户信息时遇到的错误。
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)
这是用户-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);
});
};
您的 userService 导出对象没有 findUser 方法。
如果您的原始函数名称是 findUser
,则通过添加 module.exports.findUser = findUser;
来导出它。如果您已经在导出对象,例如:
module.exports = {
foo: foo
};
只需将 findUser: findUser
添加到该对象即可。
我完成了关于 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);
});
});
};
这是我尝试 "login" 在数据库中验证用户信息时遇到的错误。
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)
这是用户-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);
});
};
您的 userService 导出对象没有 findUser 方法。
如果您的原始函数名称是 findUser
,则通过添加 module.exports.findUser = findUser;
来导出它。如果您已经在导出对象,例如:
module.exports = {
foo: foo
};
只需将 findUser: findUser
添加到该对象即可。