首页 > 解决方案 > Mongoose Model.find() 抛出 UnhandledPromiseRejectionWarning 错误

问题描述

我将 Mongoose 与 NodeJS 一起使用,我已连接到我的 Atlas 数据库并设法创建了一个包含一些条目的集合。问题是在使用 MyModel.find((err,data)=>err?console.log(err):console.log(data)) 节点时抛出错误:

UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments.
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which 
was not handled with .catch(). To terminate the node process on unhandled promise rejection.

我尝试了很多方法,包括:向和/或添加一个.then().catch()块;在异步函数中使用;删除/更改内部的回调函数。但是没有任何效果。Model.find()mongoose.connect()Model.find()Model.find

注意:其他查找方法,例如Model.findById()&Model.findOne()以某种方式起作用。

标签: javascriptnode.jsmongodbmongoose

解决方案


这个问题面临着 mongoose 6.0 版所以你只需要降级 mongoose 版本。只需运行 npm uninstall mongoose 即可卸载当前的 mongoose 版本,然后运行 ​​npm i mongoose@5.13.8 这将安装可以解决您问题的版本


推荐阅读