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.
MongooseError: Model.find() no longer accepts a callback
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.
app.get("/articles", async (req, res) => {
try {
const articles = await Article.find({ });
res.send(articles);
console.log(articles);
} catch (err) {
console.log(err);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With