我有一个Passport本地策略尝试在mongodb中查询用户:
passport.use(new LocalStrategy(
function(username, password, done){
console.log("username and password is >>>>>", username, password);
var findOne = Q.nbind(User.findOne, User);
findOne({"username": username})
.then(function(user){
console.log("inside findone user>>>>>", err, user);
if (!user) {
return done( null, false, {messge: 'This user is not registered.'});
}
if (!user.comparePasswords(password)){
return done(null, false, {message: 'This password is not correct'});
}
return done(null, user);
})
.fail(function(err){
console.log("failed at here");
return done(null, false, {message: "Server have difficulty"});
})
}
));
打印“在这里失败”,这意味着访问mongodb时出错。但是,我在注册函数中编写了非常相似的函数,并且每件事都运行良好:
exports.signup = function(req, res, next){
console.log("hit the signup function");
var findOne = Q.nbind(User.findOne, User);
findOne({username: req.body.username})
.then(function(user){
console.log("user from query is >>>>>", user);
if (!user){
// user doesn't exist, create a new one\
var create = Q.nbind(User.create, User);
var newUser = {username: req.body.username, password: req.body.password};
return create(newUser);
} else {
// user already exist, redirect to sign in page
res.send(409, 'already exist');
//res.redirect('/signin');
}
})
.then(function(user){
res.json(200,user);
})
.fail(function(err){
//next(err);
console.log("error is >>>>", err);
res.redirect('/signin');
})
};
我通过注册创建了一个用户。因此,当我尝试使用同一个用户注册时,它会给我409。但是,当我尝试使用该用户登录时,护照本地策略报告错误访问mongodb。有什么想法吗? 欢迎您查看回购:https://github.com/7seven7lst/chatterApp 护照配置可在以下位置找到:https://github.com/7seven7lst/chatterApp/blob/master/lib/routes.js 注册可以在以下位置找到: https://github.com/7seven7lst/chatterApp/blob/master/lib/controllers/user.js
答案 0 :(得分:1)
.then(function(user){
console.log("inside findone user>>>>>", err, user);
由于未定义err
,因此引发错误。
如果您记录fail
功能的实际错误:
.fail(function(err){
console.log("failed at here", err);
return done(null, false, {message: "Server have difficulty"});
})
你会看到:
ReferenceError: err is not defined
at /Users/dting/chatterApp/lib/routes.js:16:49