1

I have a user document

username: "Ralph",
email: "[email protected]",
resetPasswordToken: null,
resetPasswordExpires: null

and I want to update the resetPasswordToken and the resetPasswordExpires properties in an Express route,

I did this :

router.post("/forgotPassword", (req, res) => {
  User.findOne({ email: req.body.email }).then(user => {
    if (user === null) {
      console.log("NO user with this mail exists");
    } else {
      console.log("user with this mail exists");

      const token = crypto.randomBytes(20).toString("hex");
      const myDate = new Date();
      const newDate = new Date(myDate);

      user.update({
        resetPasswordToken: token,
        resetPasswordExpires: newDate.setHours(newDate.getHours() + 1)
      });

I see the log "user with this mail exists" in the terminal, but the user.update is never done, because there are no changes in my MongoDB database,

Does someone have a solution ? Thanks

1 Answer 1

1

FYI: .update() is deprecated by Mongo. You should switch to .updateOne()

On that note, you need to call the .updateOne() function on your model, not on the returned promise.

router.post("/forgotPassword", (req, res) => {
    // deconstruct email from form 
    const { email } = req.body

    // check if email is registered in the database
    User.findOne({ email })
    .then(user => {
        if (!user) {
            console.log("This email is not associated with a registered account");
            
            // send error message to the client
            return res.status(404).json("This email is not associated with a registered account");
        }
        
        console.log("This email exists in the database. Proceeding...")
    })
    .catch(err => {
        return res.status(500).json(err.message);
    });

    const token = crypto.randomBytes(20).toString("hex");
    const myDate = new Date();
    const newDate = new Date(myDate);

    // if the email exists, run this update to the account with the associated email
    User.updateOne({ email: "[email protected]" }, {
        $set: {
            resetPasswordToken: token,
            resetPasswordExpires: newDate.setHours(newDate.getHours() + 1)
        }
    })
    .then(updates => console.log(updates))
    .catch(err => {
        return res.status(500).json(err.message);
    });
});

Here we are querying to find the document where the email is [email protected], and the using the $set operator to update the associated values of resetPasswordToken and resetPasswordExpires to their new respective values.

Sign up to request clarification or add additional context in comments.

4 Comments

ok but I already do a User.findOne above to see if there's a user with this mail and if so, return an error message so i remove this ?
No you don't need to remove it. You said your problem was the user.update wasn't executed, so I was addressing that issue. Replace user.update(...) in your code snippet with my answer
I tried with your solution but it doesn’t want to work. But that’s weird because I have an update in my server which works 😂
I edited my answer, while refactoring your code and making a few small additions. Take a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.