提问人:sanchita bhardwaj 提问时间:11/16/2023 最后编辑:aneroidsanchita bhardwaj 更新时间:11/16/2023 访问量:22
连接到MongoDB时出错:TypeError:无法读取未定义的属性(读取“db”)
Error connecting to MongoDB: TypeError: Cannot read properties of undefined (reading 'db')
问:
const mongoDB = async () => {
try {
await mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true });
console.log('Connected to MongoDB');
const fetched_data = await mongoose.collection.db.collection("guideInfo");
fetched_data.find({}).toArray(function (err, data) {
if (err) console.log(err);
else console.log(data);
})
// Your code here
} catch (error) {
console.error('Error connecting to MongoDB:', error);
}
};
module.exports = mongoDB;
这是我期望从我的mongoDB中看到数据的代码,但它显示了一个错误,我不知道可能是什么原因
at mongoDB (C:\Users\acer\Desktop\disha\guide\backend\db.js:19:52)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
答:
0赞
NeNaD
11/16/2023
#1
取而代之的是:
await mongoose.collection.db.collection("guideInfo");
试试这个:
await mongoose.connection.db.collection("guideInfo");
评论
0赞
sanchita bhardwaj
11/16/2023
哦,谢谢你,它起作用了,但是终端没有显示我在该特定集合中的信息,为什么会这样?
评论