Monogodb connection error

C:\Users\BHANUCHENNAM\Desktop\mernboot\projbackend\node_modules\mongoose\lib\connection.js:579
throw new MongooseError('The uri parameter to openUri() must be a ’ +
^
Error [MongooseError]: The uri parameter to openUri() must be a string, got “undefined”. Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string.
at new MongooseError (C:\Users\BHANUCHENNAM\Desktop\mernboot\projbackend\node_modules\mongoose\lib\error\mongooseError.js:10:11)
at NativeConnection.Connection.openUri (C:\Users\BHANUCHENNAM\Desktop\mernboot\projbackend\node_modules\mongoose\lib\connection.js:579:11)
at Mongoose.connect (C:\Users\BHANUCHENNAM\Desktop\mernboot\projbackend\node_modules\mongoose\lib\index.js:333:15)
at Object. (C:\Users\BHANUCHENNAM\Desktop\mernboot\projbackend\app.js:12:6)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
message: ‘The uri parameter to openUri() must be a string, got “undefined”. Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string.’,
name: ‘MongooseError’
}
[nodemon] app crashed - waiting for file changes before starting…

It’s impossible to say what happened here without seeing your code, but my best guess is that your Mongoose connection string is using an environment variable that isn’t being read properly. If you post your Mongoose connection code, this will be much easier to diagnose.

mongoose.connect(process.env.MONGODB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true,
  useFindAndModify: false,
});

This is the part we’re looking for. If you are using an environment variable, remember to install the dotenv package with npm install dotenv and read it in the file with your Mongoose connection, require("dotenv").config().

// app.js(file name)

require(“dotenv”).config();
const mongoose = require(“mongoose”);
const express = require(“express”);
const app = express();
const bodyParser = require(“body-parser”);
const cookieParser = require(“cookie-parser”);
const cors = require(“cors”);
const authRoutes = require("./routes/auth");

//DB Connection
mongoose
.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
.then(() => {
console.log(“DB CONNECTED”);
});

//Middlewares
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());

//My Routes
app.use("/api", authRoutes);

//PORT
const port = process.env.PORT || 8000;

//Starting a server
app.listen(port, () => {
console.log(app is running at ${port});
});

//(auth.js) file name

var express = require(“express”);
var router = express.Router();

router.get("/signout", (req, res) => {
res.send(“user signout”);
});

module.exports = router;

(.env)

DATABASE=mongodb://localhost:27017/tshirt

Try putting your DATABASE variable in quotes.
DATABASE='mongodb://localhost:27017/tshirt'

tried getting the samething…

I know it’s always disappointing to hear, but I when I use this code, it works just fine for me, (except for some reason my editor didn’t recognize any of the quotation marks, so I had to replace them). I did get rid of your routing when I tried it to avoid having to build things that didn’t affect the DB connection.
So here’s what I ended up with:

require("dotenv").config();
const mongoose = require("mongoose");
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");

//DB Connection
mongoose
  .connect(process.env.DATABASE, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
  })
  .then(() => {
    console.log("DB CONNECTED");
  });

//Middlewares
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());

//PORT
const port = process.env.PORT || 8000;

//Starting a server
app.listen(port, () => {
  console.log(`app is running at ${port}`);
});

And my .env file:

DATABASE='mongodb+srv://<myUsername>:<myPassword>@cluster0-bqrlk.mongodb.net/test?retryWrites=true&w=majority'

Have you tried console logging the value of process.env.DATABASE before passing it to the mongoose.connect to make sure it’s a valid string?