英文:
Unable to access data from mongoDB?
问题
我按照教程进行了操作,通过mongoose连接到了mongoDB,但出于某种原因,console.log(data)没有显示任何内容。
英文:
I followed a tutorial, connected to mongoDB via mongoose, but for some reason the console.log(data) doesn't show anything.
const mongoose = require('mongoose');
const mongoURI = 'mongodb+srv://gofood:xxxxxxxxx@cluster0.bgpaa3e.mongodb.net/?retryWrites=true&w=majority'
process.on('uncaughtException', err => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
async function called(){
console.log('Connection successful!');
const fetched_data = await mongoose.connection.db.collection("foodCategory");
console.log('reaching here?????')
fetched_data.find({}).toArray(function(err, data){
if(err) console.log(err);
else console.log(data);})
};
const mongoDB = async () => {
await mongoose
.connect(mongoURI, {
useNewUrlParser: true
})
.then(() => called())
}
module.exports = mongoDB;
output:-
[nodemon] starting `node .\index.js`
Example app listening on port 5000
Connection successful!
reaching here?????
In the tutorial the data comes up instantly. Is there something that I'm not doing?
答案1
得分: 1
尝试使用 await 直到找到所有数据
// language: lang-js
async function called(){
console.log('连接成功!');
const fetched_data = await mongoose.connection.db.collection("foodCategory");
console.log('是否到达这里?????')
await fetched_data.find({}).toArray(function(err, data){
if(err) console.log(err);
else console.log(data);})
};
英文:
Try await until u find all the data
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
async function called(){
console.log('Connection successful!');
const fetched_data = await mongoose.connection.db.collection("foodCategory");
console.log('reaching here?????')
await fetched_data.find({}).toArray(function(err, data){
if(err) console.log(err);
else console.log(data);})
};
<!-- end snippet -->
答案2
得分: 0
你可以像这样修改 called() 函数:
async function called() {
console.log('连接成功!');
const FoodCategory = mongoose.model('FoodCategory', new mongoose.Schema({}));
const data = await FoodCategory.find().exec();
console.log(data);
}
也许会起作用!
英文:
You can modify the called() function like this:
async function called() {
console.log('Connection successful!');
const FoodCategory = mongoose.model('FoodCategory', new mongoose.Schema({}));
const data = await FoodCategory.find().exec();
console.log(data);
}
May be it's work!!
答案3
得分: 0
这是我修复它的方式。
我在我的函数中提供了集合名称,但没有提供数据库名称,后来我发现在MongoDB的URL中,你必须提到数据库名称。
旧的URL
新的URL
英文:
This is how i fixed it.
I was wondering that in my function i'm providing my collection name but not database name. later i found that in the mongoDB url you have to mention the database name
old url
const mongoURI = 'mongodb+srv://gofood:xxxxxxxxx@cluster0.bgpaa3e.mongodb.net/?retryWrites=true&w=majority'
new url
const mongoURI = 'mongodb+srv://gofood:xxxxxx@cluster0.bgpaa3e.mongodb.net/**gofoodmern**?retryWrites=true&w=majority'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论