i have been working in a Node js small project where i use MongoDB as Database and use mongoose as a Data Modeling Library.
but when i tried to connect to Database its show an which is: Mongoose Error 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
the actual error screen shot is:
after seen this type of error i want to fix why this is happen cause i am 100% confident that there is no any coding issues and i have written well organize code. so i try to find out the problem after few times i got what i did and what is my mistake and then i solve the issue and i decide to share this solution with all of you the internet people.
So, to solve 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
i go to my API index file and place the dbConfig.js file under the dotenv config file. so why dotenv because i have created env file and use dotenv libraray.
and now i want to show 2 screen shot where the first one is hold the Mongoose Error: mongoose.connect or mongoose.createConnection is a string
and the second one is the solution of this type of error i men to say what i exactly did to avoid this type of error.
1 . Screen shot of the code which generate Mongoose connection String error:
2. Screen shot of the code How i solve or avoid Mongoose connection String error:
this all solve my Mongoose Error: Connection String Error. and after solution the terminal scenario is:
.
That's it
Hope this will help you
Happy Coding
.
osman forhad
Full-Stack Developerđź’» (Mobile App & Web App)
developer.osmanforhad@gmail.com
Top comments (1)
index.js
//entry point of our appn
import express from "express";
import dotenv from "dotenv"
import { connectDB } from "./lib/db.js";
import userRoutes from "./routes/user.route.js";
import adminRoutes from "./routes/admin.route.js";
import authRoutes from "./routes/auth.route.js"
import songRoutes from "./routes/song.route.js"
import albumRoutes from "./routes/album.route.js"
import statRoutes from "./routes/stat.route.js"
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000
app.use("/api/users", userRoutes)
app.use("/api/admin", adminRoutes)
app.use("/api/auth", authRoutes)
app.use("/api/songs", songRoutes)
app.use("/api/albums", albumRoutes)
app.use("/api/stats", statRoutes)
app.listen(PORT, ()=>{
console.log("Server is running on port " + PORT);
connectDB();
})
db.js
import mongoose from "mongoose";
export const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGODB_URI);
console.log(
Connected to MongoDB ${conn.connection.host}
);}
.env.local
VITE_CLERK_PUBLISHABLE_KEY=pk_test_bWFnaW*********************************************************************
MONGODB_URI=mongodb+srv://***********************************.mongodb.net/spotify_db?retryWrites=true&w=majority&appName=Cluster0
Error