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.
8
Answers
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
Hope it helped!
Since the callback function has been deprecated from now onwards.
If you are using these functions with callbacks, use async/await or promises if async functions don’t work for you.
Good day to everyone! Mongoose has just made a new update, mongoose has now got rid of callbacks. So that means, now instead of doing this
You should do it like this!
This also applies for every other mongoose callback
Anyone whose facing issue connecting to mongodb. Just use async await and you are good to go…
You can either use then catch or async await
then catch method:
async await method: