Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
I am using MongoDB atlas
Index.js
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
import postRoutes from "./Routes/posts.js";
import dotenv from "dotenv";
const app = express();
dotenv.config();
app.use(bodyParser.json({ limit: "50mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(cors());
app.use("/posts", postRoutes);
const PORT = process.env.PORT || 4000;
mongoose
.connect(process.env.CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(PORT, console.log(Server is running on port ${PORT}));
})
.catch((err) => console.log(err.message));
.env file
CONNECTION_URL =
"mongodb+srv://<UserName>:<Password>@cluster0.v5qzigz.mongodb.net/?retryWrites=true&w=majority";
PORT = 4000;
it works fine if i do not use .env File. Process.env.CONNECTION_URL is not working ?
If the <Username> or <Password> includes those following characters, simply replace them.
: / ? # [ ] @