I’ve been using callbacks for .save() and .findOne() for a few days now and just today I encounter these errors:
throw new MongooseError('Model.prototype.save() no longer accepts a callback')
MongooseError: Model.prototype.save() no longer accepts a callback
and
MongooseError: Model.findOne() no longer accepts a callback
It’s really awkward given that callbacks are still accepted in the docs at least for .findOne().
app.post("/register", (req, res) => {
const newUser = new User({
email: req.body.username,
password: req.body.password
});
newUser.save((err) => {
if (err) console.log(err)
else res.render("secrets");
});
});
This is what used to work for me, using express and mongoose. Please let me know how to fix it.
>Solution :
app.post("/login",function(req,res){
const username = req.body.username;
const password = req.body.password;
User.findOne({email:username})
.then((foundUser) => {
if(foundUser){
if(foundUser.password === password){
res.render("secrets");
}
}
})
.catch((error) => {
//When there are errors We handle them here
console.log(err);
res.send(400, "Bad Request");
});
});
This works for me, also im doing the web dev bootcamp by angela yu.
It seems like now you have to use => then and =>catch for the handling