Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  86] [ 3]  / answers: 1 / hits: 25715  / 1 Year ago, mon, february 27, 2023, 12:00:00

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.


More From » mongodb

 Answers
0

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);
}
});

[#50002] Friday, June 3, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryans

Total Points: 514
Total Questions: 92
Total Answers: 121

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
;