Home > other >  I am learning mongodb but getting this error [closed]
I am learning mongodb but getting this error [closed]

Time:09-16

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Listening on localhost:${port}
(node:137705) UnhandledPromiseRejectionWarning: MongoParseError: option usecreateindex is not supported
    at Object.parseOptions (/home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongodb/lib/connection_string.js:281:15)
    at new MongoClient (/home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongodb/lib/mongo_client.js:62:46)
    at /home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongoose/lib/connection.js:781:16
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongoose/lib/connection.js:778:19)
    at /home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongoose/lib/index.js:330:10
    at /home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/home/nayan/Documents/whatsapp clone/whatsapp-backend/node_modules/mongoose/lib/index.js:1151:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:137705) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:137705) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

CodePudding user response:

Remove usecreateindex and try to make connection like this:

const URI = process.env.MONGODB_URL;
mongoose.connect(URI, {
   // useCreatendex: true, 
   // useFindAndModify: false, 
   useNewUrlParser: true, 
   useUnifiedTopology: true 
}, err => {
   if(err) throw err;
   console.log('Connected to MongoDB!!!')
}) 

CodePudding user response:

Try this one out, this basically is connecting to the .env where the MONGO_URI exists there

backend/config/db.js:

require('dotenv').config();
const mongoose = require('mongoose');

// connect to DB, by cathing the URI from the databse MongoDB

const connectDB = async () => {
    try {
        await mongoose.connect(process.env.MONGO_URI,{
            useNewUrlParser: true,
            useUnifiedTopology: true
        });

        console.log("MongoDB connection success");
    } catch (error) {
        console.error("MongoDB connection Fail");
        process.exit(1); // exits the server with 1 
        
    }

}


 
module.exports = connectDB

.env:

NODE_ENV=development  

PORT=5000

MONGO_URI="Your MongoDB URL"
  • Related