TypeError:conn.db不是函数



当我尝试连接到mongodb数据库中的集合时,会出现以下错误:TypeError:conn.db不是函数

PS:我使用的是4.4.6版

谢谢你的帮助!

export default class OfferingDAO {
static async injectDB(conn){
if(offerings){
return;
}
try { 
offerings = await conn.db(process.env.NS).collection("Offering");
}
catch(e){
console.error(`Unable to establish a collection handle in OfferingDAO: ${e}`);
}
}

这是我连接mongodb的代码:

await mongoose.connect(process.env.URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
})
.then(async (client) => {
await OfferingDAO.injectDB(client);
app.listen(port, () => {
console.log(`listening on port ${port}`)
}) 
})

根据文档,客户端仅是您的数据库实例:

// Using `mongoose.connect`...
var promise = mongoose.connect('mongodb://localhost/myapp', {
useMongoClient: true,
/* other options */
});
promise.then(function(db) {
/* Use `db`, for instance `db.model()`
});

因此,如果您的代码更新为:,那么它应该可以工作

offerings = await conn.collection("Offering");

最新更新