I'm currently working on a validation system using Node, Express and Mongoose and have stumbled into a bit of problem. In my Schema I have a verificationId associated with the user so that when they click the link emailed to them it can check to see if that verificationId is the same as the one in the database. All of that works fine, but I can't figure out how to delete the verificationId now that it's no longer needed.
Currently it's validating the user but not deleting verificationId. I've tried messing with the $pull method but I haven't had any success with it. Any help would be appreciated!
//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 }
) });
});
I'm not very sure about this answer but try using the unset operator:
or this may work ( setting the value to null )