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:[email protected]/?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?
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:[email protected]/?retryWrites=true&w=majority'
new url
const mongoURI = 'mongodb+srv://gofood:[email protected]/**gofoodmern**?retryWrites=true&w=majority'