MongoDB返回一个不存在的onject



我正在使用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会返回一个您没有等待的Promise。所以当您检查p1和p2是否存在时,它会返回一个truthy值,因为这两个值都是Promise对象。

要解决这个问题,请在两个User.find前面使用wait,就像这个一样

const p1 = await User.find({ p1: req.body.p1 });
const p2 = await User.find({ p2: req.body.p2 });

之后,两个值都将是数组,因为您使用find方法,所以只需检查length属性,或者最好使用findOne而不是find方法。

const p1 = await User.findOne({ p1: req.body.p1 });
const p2 = await User.findOne({ p2: req.body.p2 });
  1. MongoDB.find返回一个数组。在您的情况下,p1是一个空数组。if(p1)将始终返回true。你应该检查一下它的长度
  2. 您应该使用await进行查询调用。
    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);
});
});
}
)
);

最新更新