Home > Enterprise >  Unable to connect mongoDb to Express
Unable to connect mongoDb to Express

Time:02-20

I am making a registration form with the MERN stack, I am connecting Express app to mongoDB but I am getting error in connection. I am following Thapa Technical video on YouTube, and I have done exactly what Thapa did, I don't know what is wrong in this, I am a beginner please help me.

app.js file:

const express = require('express');
const app = express();
require("./db/conn")

const port = process.env.PORT || 3000;

app.get("/", (req, res) => {
        res.send("Welcome to Abdullah's webbsite")
    });
    
    app.listen(port, () => {
            console.log(`Server is running at port no ${port}`)
        })

conn.js file:

const mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/userRegistration", {
    useNewUrlParser:true,
    useUnifiedTopology:true,
    useCreateIndex:true
}).then(() =>{
    console.log(`Connection Successful`);
}).catch((e) => {
    console.log(e);
})

package.json file:

{
  "name": "registration-form",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev" : "nodemon src/app.js"
  },
  "author": "Abdullah",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.3",
    "hbs": "^4.2.0",
    "mongoose": "^6.2.2"
  }
}

console:

    Server is running at port no 3000
MongoParseError: option usecreateindex is not supported
    at parseOptions (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongodb\lib\connection_string.js:289:15)
    at new MongoClient (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongodb\lib\mongo_client.js:62:63)
    at C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\connection.js:784:16
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\connection.js:781:19)
    at C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\index.js:340:10
    at C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\index.js:1140:10)

CodePudding user response:

Please remove useCreateIndex option in connn.js:

mongoose.connect("mongodb://localhost:27017/userRegistration", {
    useNewUrlParser:true,
    useUnifiedTopology:true
}).then(() =>{
    console.log(`Connection Successful`);
}).catch((e) => {
    console.log(e);
})

CodePudding user response:

const express = require('express');
const mongoose = require('mongoose');

const app = express();
app.use(express.json());


let userModel;

// configure mongoose
mongoose.connect('mongodb://localhost:27017/userRegistration', (err) => {
  if (err) {
    console.log(`Error connecting to mongodb => `, err);
  } else {
    console.log(`Mongodb Connected successfully`);
  }
});
  • Related