Reputation: 637
I'm currently working on adding a "forgot-password"-functionality and am facing a problem which I did not manage to solve on my own.
In my mongoose findOneAndUpdate-function, I noticed that it updates an entry even though said entry doesn't exist.
Here's my function :
router.put('/resetpass', (req, res) => {
const {resetPassLink, password} = req.body;
Users.findOneAndUpdate({resetPassLink : resetPassLink}, {$set : {password : password, resetPassLink: ''}}, function(error, feedback) {
if (error) throw error;
return res.send(feedback);
})
})
This post suggested that the user who asked the question, encountered the problem because his function did not produce any errors, however, mine does produce an error.
I'm not sure about what's going wrong in my code. Shouldn't it throw the error once it notices that there's no resetPassLink-field with the value it is looking for?
What exactly am I doing wrong and how do I fix it?
Upvotes: 0
Views: 960
Reputation: 2111
You need to improve your query little bit like below :
router.put('/resetpass', (req, res) => {
const {resetPassLink, password} = req.body;
Users.findOneAndUpdate({resetPassLink : resetPassLink, resetPassLink:
{$exists: true}, password: {$exists: true}}, {$set :
{password : password, resetPassLink: ''}}, (error, feedback) => {
if (error) {
console.log(error)
}; else {
return res.send(feedback);
}
});
Upvotes: 2