我在一个MongoDB和passport的项目中工作,当我遇到这个错误时,事件虽然没有使用p1,但它仍然会重新运行一个我猜测的对象,因为它只是说字段p1被取了,当它没有被取的时候。P2也是如此。有人知道为什么吗?
passport.use(
"local.signup",
new LocalStrtegy(
{
usernameField: "email",
passwordField: "password",
passReqToCallback: true,
},
async function (req, email, password, done) {
req.checkBody("email", "E-mail is empty").notEmpty();
req
.checkBody("password", "Your password is too short!")
.isLength({ min: 4 });
var errors = await req.validationErrors();
if (errors) {
var messages = [];
errors.forEach(function (error) {
messages.push(error.msg);
});
return done(null, false, req.flash("error", messages));
}
const p1 = User.find({ p1: req.body.p1 });
const p2 = User.find({ p2: req.body.p2 });
User.findOne({ email: email }, function (err, user) {
if (err) {
return done(err);
}
if (user) {
return done(null, false, {
message:
"This E-Mail alredy in use! If you believe that this is an error, please an admin on. (ERR 002 MCEE)",
});
} else if (p1) {
return done(null, false, {
message:
"This username is alredy in use! If you believe that this is an error, please contact an admin. (ERR 002 MCEM)",
});
} else if (p2) {
return done(null, false, {
message:
"This Tag is alredy in use! If you believe that this is an error, please contact an admin. (ERR 002 MCED)",
});
}
console.log(mc + " " + dcign + " " + user);
var newUser = new User();
newUser.email = email;
newUser.password = newUser.encryptPassword(req.body.password);
newUser.p1 = req.body.p1;
newUser.p2 = req.body.p2;
newUser.Banned = false;
console.log(req.body);
newUser.save(function (err, result) {
if (err) {
return done(err);
}
return done(null, newUser);
});
});
}
)
);
调用user.find返回一个您不等待的承诺。因此,当您检查p1和p2是否存在时,它返回一个truthy值,因为这两个值都是Promise对象。
若要修复此问题,请在两个用户前面使用await.如下所示查找
const p1 = await User.find({ p1: req.body.p1 });
const p2 = await User.find({ p2: req.body.p2 });
之后,这两个值都将是数组,因为您正在使用find方法,所以只需检查长度属性,或者更好地使用findOne而不是find方法。
const p1 = await User.findOne({ p1: req.body.p1 });
const p2 = await User.findOne({ p2: req.body.p2 });
.find
returns an array. In your case p1 is an empty array. if(p1)
will always return true. You should check for its length.await
for your query calls. const p1 = await User.find({ p1: req.body.p1 });
const p2 = await User.find({ p2: req.body.p2 });
我在下面粘贴示例代码-
passport.use(
"local.signup",
new LocalStrtegy(
{
usernameField: "email",
passwordField: "password",
passReqToCallback: true,
},
async function (req, email, password, done) {
req.checkBody("email", "E-mail is empty").notEmpty();
req
.checkBody("password", "Your password is too short!")
.isLength({ min: 4 });
var errors = await req.validationErrors();
if (errors) {
var messages = [];
errors.forEach(function (error) {
messages.push(error.msg);
});
return done(null, false, req.flash("error", messages));
}
const p1 = await User.find({ p1: req.body.p1 });
const p2 = await User.find({ p2: req.body.p2 });
User.findOne({ email: email }, function (err, user) {
if (err) {
return done(err);
}
if (user) {
return done(null, false, {
message:
"This E-Mail alredy in use! If you believe that this is an error, please an admin on. (ERR 002 MCEE)",
});
} else if (p1.length) { // Check for Length
return done(null, false, {
message:
"This username is alredy in use! If you believe that this is an error, please contact an admin. (ERR 002 MCEM)",
});
} else if (p2.length) { // Check for Length
return done(null, false, {
message:
"This Tag is alredy in use! If you believe that this is an error, please contact an admin. (ERR 002 MCED)",
});
}
console.log(mc + " " + dcign + " " + user);
var newUser = new User();
newUser.email = email;
newUser.password = newUser.encryptPassword(req.body.password);
newUser.p1 = req.body.p1;
newUser.p2 = req.body.p2;
newUser.Banned = false;
console.log(req.body);
newUser.save(function (err, result) {
if (err) {
return done(err);
}
return done(null, newUser);
});
});
}
)
);