Javascript 未删除阵列中的Mongoose验证系统元素
我目前正在使用Node、Express和Mongoose开发一个验证系统,遇到了一些问题。在我的模式中,我有一个与用户关联的verificationId,这样当用户单击通过电子邮件发送给他们的链接时,它可以检查该verificationId是否与数据库中的相同。所有这些都很好,但我不知道如何删除verificationId,因为它不再需要了 当前它正在验证用户,但没有删除verificationId。我尝试过使用$pull方法,但没有成功。任何帮助都将不胜感激Javascript 未删除阵列中的Mongoose验证系统元素,javascript,node.js,mongodb,validation,mongoose,Javascript,Node.js,Mongodb,Validation,Mongoose,我目前正在使用Node、Express和Mongoose开发一个验证系统,遇到了一些问题。在我的模式中,我有一个与用户关联的verificationId,这样当用户单击通过电子邮件发送给他们的链接时,它可以检查该verificationId是否与数据库中的相同。所有这些都很好,但我不知道如何删除verificationId,因为它不再需要了 当前它正在验证用户,但没有删除verificationId。我尝试过使用$pull方法,但没有成功。任何帮助都将不胜感激 //User verificati
//User verification page
app.get("/verify/users/:verifiedId", (req, res) => {
const verifiedId = req.params.verifiedId;
//Check to see if the verificationHash the user was sent is the same as the one stored for them in the database
User.findOne({ verificationHash: verifiedId }, (err, result) => {
if (!err) {
console.log(verifiedId);
console.log(result);
const originalValue = { isVerified: false };
const newValue = { isVerified: true };
//Verify the user in the database
User.findOneAndUpdate(originalValue, newValue, (err) => {
if (!err) {
if (newValue) {
res.redirect("/success");
} else {
res.send(
"There was an error verifying your account. Please try again."
);
}
} else {
res.send(500, { error: err });
}
});
} else {
res.send(err);
console.log(err);
console.log(verifiedId);
}
//Delete the verificationHash from the user in the database
User.findOneAndUpdate(
{ verificationHash: verifiedId },
{ $pull: { verificationHash: { verificationHash: verifiedId } } },
{ new: true }
) });
});
我不太确定这个答案,但请尝试使用:
User.findOneAndUpdate(
{ verificationHash: verifiedId },
{ { $unset: {"verificationHash": ""} },
{ new: true }
)
或者这可能会起作用(将值设置为null)
User.findOneAndUpdate(
{ verificationHash: verifiedId },
{ verificationHash: null },
{ new: true }
)