This is the error;
PS D:\MERN STACK\backend> npm run dev
backend@1.0.0 dev
nodemon server.js
[nodemon] 3.0.1
[nodemon] to restart at any time, enter rs
[nodemon] watching path(s): .
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting node server.js
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 NativeConnection.createClient (D:\MERN STACK\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:206:11)
at NativeConnection.openUri (D:\MERN STACK\backend\node_modules\mongoose\lib\connection.js:755:34)
at Mongoose.connect (D:\MERN STACK\backend\node_modules\mongoose\lib\index.js:404:15)
at Object. (D:\MERN STACK\backend\server.js:24:10)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes…
[nodemon] starting node server.js
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 NativeConnection.createClient (D:\MERN STACK\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:206:11)
at NativeConnection.openUri (D:\MERN STACK\backend\node_modules\mongoose\lib\connection.js:755:34)
at Mongoose.connect (D:\MERN STACK\backend\node_modules\mongoose\lib\index.js:404:15)
at Object. (D:\MERN STACK\backend\server.js:24:10)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47
[nodemon] clean exit - waiting for changes before restart
server.js file :
require('dotenv').config();
const express = require('express')
const mongoose = require('mongoose')
mongoose.set('strictQuery', false);
const workoutRoutes = require('./routes/workouts')
// express app
const app = express()
// middleware
app.use(express.json())
app.use((req, res, next) => {
console.log(req.path, req.method)
next()
})
// routes
app.use('/api/workouts', workoutRoutes)
// connect to db
mongoose.connect(process.env.MONGO_URI)
.then(() => {
// listen to port
app.listen(process.env.PORT, () => {
console.log('listening for requests on port', process.env.PORT)
})
})
.catch((err) => {
console.log(err)
})
workouts.js file:
const express = require('express');
const router = express.Router();
//GET all workouts
router.get('/',(req,res)=>{
res.json({mssg: 'Get all workouts'});
})
//get a single workout
router.get('/:id',(req,res)=>{
res.json({mssg: 'Get a single workout'});
})
//POST a new workout
router.post('/',(req,res)=>{
res.json({mssg: 'POST a new workout'});
})
//DELETE a workout
router.delete('/:id',(req,res)=>{
res.json({mssg: 'DELETE a workout'});
})
//UPDATE a workout
router.patch('/:id',(req,res)=>{
res.json({mssg: 'UPDATE a workout'});
})
module.exports = router;
.env file:
PORT=4000
MONG_URI="mongodb+srv://muhammadmusab846:<password>@cluster0.hk5nwf5.mongodb.net/?retryWrites=true&w=majority"
I’ve checked everything but couldn’t get rid of this error. Can somebody help me resolving this error? I am stuck.