javascript - 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()
以某种方式起作用。
解决方案
这个问题面临着 mongoose 6.0 版所以你只需要降级 mongoose 版本。只需运行 npm uninstall mongoose 即可卸载当前的 mongoose 版本,然后运行 npm i mongoose@5.13.8 这将安装可以解决您问题的版本
推荐阅读
- angular - mat-form-field legacy 更改边框颜色
- c++ - 这是 g++ for 循环实现还是我的代码中的错误
- android - Flutter 中 Android 中按钮的“onclick”功能
- c# - Angular JS - 将字符串传递给 GET 方法并更新
- google-cloud-dataflow - 使用 gcloud 为数据流作业传递参数列表的语法
- vba - VBA 复选框不会将状态设置为 TRUE,但会设置为 FALSE
- c# - 为什么列表中的内容不改变?
- powershell - 继续查询 Powershell 开关参数
- python - 无法在vs代码中编译cpp和python文件
- java - 如何为 Amazon SNS (Java) 设置超时?