mongoose 6.0.2 中使用 collection.find()遇到 warning
代码场景#
const mongoose = require('mongoose');
//链接本地数据库
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
.then((result) => {
console.log('数据库连接成功');
}).catch((err) => {
console.log('数据库连接失败')
});
//创建集合规则
const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
//创建集合
const User = mongoose.model('User', userSchema);
User.create({name: '李万年', age: '30'}).then(result => console.log(result));
//此处代码块报错!
User.find({ name: '李万年' }).then(result => {
console.log(result);
})
报错信息:#
(node:12292) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
at Collection.find (D:\gh_09\桌面\test\database\node_modules\mongodb\lib\collection.js:238:19)
at NativeCollection.<computed> [as find] (D:\gh_09\桌面\test\database\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33)
at NativeCollection.Collection.doQueue (D:\gh_09\桌面\test\database\node_modules\mongoose\lib\collection.js:135:23)
at D:\gh_09\桌面\test\database\node_modules\mongoose\lib\collection.js:82:24
at processTicksAndRejections (internal/process/task_queues.js:75:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12292) 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, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12292) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
解决方案#
这个问题是在 mongoose 6.0 版本中遇到的,所以你只需要降级 mongoose 版本。只需运行 npm uninstall mongoose 来卸载当前的 mongoose 版本,然后运行 npm i mongoose@5.13.8 这将安装可以解决您的问题的版本。
javascript - Error when trying to use .find() in Mongoose - Stack Overflow
作者:Hong•Guo
出处:https://www.cnblogs.com/ghnb1/p/15856513.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
分类:
问题记录
Buy me a cup of coffee ☕.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix