这个api
文档说client
属性是MongoClient
示例-https://mongoosejs.com/docs/api/connection.html#Connection()
当我运行下面的代码时,我在尝试使用clent -something bad happened in dbconnect: TypeError: Cannot read properties of undefined (reading 'db')
获取数据库名称时出错。这是因为client
是undefined
。
const connection = await mongoose.connect("mongodb://0.0.0.0:27017");
console.log('connected to Mongo');
const client = connection.client; //SHOULD GET MONGO CLIENT
try {
const db = client.db("admin");
console.log(`connected to database ${db.databaseName}`);
const collections = await db.collections();
collections.forEach(c => {console.log(`got collection ${c.collectionName}`)})
} catch(exception) {
console.log(`something bad happened in dbconnect: ${exception}`); //ERROR
} finally {
console.log("closing database connection");
connection.disconnect();
}
字符串
我错过了什么?
1条答案
按热度按时间6rqinv9w1#
我想你想要
connection.connections[0].client
像这样:字符串