翻译一下,mongoose新版7.x废弃了find()\save()方法回调,可以修改为try catch,代码示例如下。
但最简单的就是把mongoose降低到6.10版本就可以了
npm uninstall mongoose
安装
npm install mongoose@6.10.0
完事。
Use async/await or promises for Mongoose functions
As Mongoose Released Version 7.x They Deprecated Some Functions As Per Official Documents functions no longer accept callbacks. They always return promises. To Solve MongooseError: Model.find() no longer accepts a callback error You need to Use async/await or promises for Mongoose functions. As Like Given below.
app.get("/posts", async (req, res) => {
try {
const posts= await Posts.find({ });
res.send(posts);
console.log(posts);
} catch (err) {
console.log(err);
}
});
Or You are Using Save Function then You Need to Use the async function. As I Have Given Below.
// You need to use Async Function
const result = await data.save()
console.log(result);
And Now, Your error will be Solved.
Downgrade mongoose To 6.x
If You do not want to change the Function callback then You can Downgrade Your mongoose to Version 6.x. First of all, uninstall mongoose By running the npm command.
npm uninstall mongoose
And now, You need to install mongoose 6.x.
npm install mongoose@6.10.0
And now You can use the callback function without facing any error.
参考MongooseError: Model.find() no longer accepts a callback - Pincods.com