I want to connect to Mongodb with Thunder client extension in Vs code.
Here's what i have got so far, but now I'm getting an error while connecting the app gets crashed.
const mongoose = require('mongoose');
const mongoURI = "mongodb+srv://****:****@cluster0.jswzjap.mongodb.net"
const connectToMongo = ()=>{
mongoose.connect(mongoURI, ()=>{
console.log("Connected to Mongo Successfully");
})
}
module.exports = connectToMongo;
This is the code for the connection string
D:\I NOTE2\inote3\backend>nodemon ./index.js
[nodemon] 3.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node ./index.js`
Example app listening on port http://localhost:3000
{ name: 'xyz', email: '[email protected]', password: '4567' }
D:\I NOTE2\inote3\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:186
const err = new MongooseError(message);
^
MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (D:\I NOTE2\inote3\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:186:23)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)
Node.js v18.17.1
[nodemon] app crashed - waiting for file changes before starting...
This is the error I'm getting.
Change your
db.jsfile to anasyncfunction:Then inside your
index.jsyou need to call the function. Just importing it usingrequireisn't enough: