javascript - MongoDB 返回一个不存在的对象
问题描述
我正在使用 MongoDB 和护照做一个项目,当我遇到这个错误时,虽然没有使用 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);
});
});
}
)
);
解决方案
- MongoDB
.find
返回一个数组。在您的情况下, p1 是一个空数组。if(p1)
将始终返回 true。你应该检查它的长度。 - 您应该使用
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);
});
});
}
)
);
推荐阅读
- sql - 如何使用 SQL 和内联视图来完成这个特定的问题/查询?
- r - 从 RStudio Cloud 中的 FTP 下载 .zip 文件
- c# - Unity:衬里渲染器和对象在几乎超出屏幕时消失
- ubuntu - 在 Google Compute Engine 中访问附加磁盘的内容
- docker - 如何将 Jet Brains DataGrip 连接到 Elasticsearch 实例?
- reactjs - “any”类型的参数不能分配给“never”类型
- java - 如果条件为真,但它会进入其他 Android Studio
- objective-c - 将 Caffe MemNet 模型转换为 coreML
- testing - 颤振测试失败。找不到编译器错误获取器'main'
- java - java中两个数组的联合没有得到想要的结果