方法"collection.find()"最多接受两个参数


const roomSchema = new mongoose.Schema({
roomname: {
type: String,
required: true
},
users: {
type: Array,
required: true
},
createdDate: {
type: Date,
default: new Date()
},
admins: {
type: Array
},
groupType: {
type: String,
required: true
},
requests: {
type:Array
},
ip: {
type:String,
required:true
},
country:{
type:String,
required:true
}
});
const roomModel=mongoose.model('room',roomSchema)
const getUsersInaRoom=async ()=>{
const res = await roomModel.find({ 'roomname': 'room1' });
console.log(res);
}
getUsersInaRoom();

抛出的错误是:

node:6123) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()"最多接受两个参数在收集。找到(/用户/macpro/桌面/反应/chat-using-socket/后端/node_modules/mongodb/lib/collection.js: 238:19)

几分钟前它还能正常工作,现在抛出上面的错误。

新版本的mongoose似乎有一个问题。

执行npm rm mongoose命令卸载当前版本,执行npm i mongoose@5.13.8命令安装旧版本后重试。

检查在连接MongoDB时是否指定了以下一些选项:useNewUrlParser,useUnifiedTopology,useFindAndModify,useCreateIndex。如果是,请将这些选项从选项对象中删除,然后再试一次。

如果你想找到一个,只需使用findOne({})

const getUsersInaRoom = async() => {
const res = await roomModel.findOne({ 'roomname': 'room1' });
console.log(res);
}

我认为键不应该是字符串。

的例子:

// const res = await roomModel.find({ 'roomname': 'room1' });
const res = await roomModel.find({ roomname: 'room1' });

当您在mongoose.connect()成功之前调用find()时会发生此问题。在调用find()之前等待mongoose.connect()。

最新更新