我正在尝试将函数的参数传递给 mongodb 集合 find。像这样:
async find() {
try {
return await db.collection('users').find.apply(null, arguments);
} catch(err) {
console.error(err);
}
}
返回
TypeError: Cannot read property 's' of null at Collection.find (/localpath/node_modules/mongodb/lib/collection.js:282:22)
我可以跑
await db.collection('users').find()
并且光标正在返回。因此连接和收集已正确设置。
我在这里没有得到什么?
最佳答案
您要应用的第一个参数为空,但不可能。
我建议您将引用作为第一个参数传递给 apply
,或者改为解开参数:
await db.collection('users').find(...arguments)
<小时/>
澄清 null 作为 apply
的第一个参数有何影响......
// will log "hello"
({foo:function(){console.log(this.bar)}, bar:'hello'}).foo()
// will log undefined
({foo:function(){console.log(this.bar)}, bar:'hello'}).foo.apply(null)
关于javascript - mongodb 将参数传递给集合 find,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44847956/