Hi guys,
I am trying to complete the Exercice tracker project for the Backend development certification. Unfortunatelty I struggle to connect to the mongodb database because I have this error: " MongooseError: The uri
parameter to openUri()
must be a string, got “undefined”
I double checked my code several times and I don’t understand why I get this. Last time it worked once but now it does not work anymore.
My code so far
Js code:
const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const mongoose = require('mongoose')
app.use(cors());
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true } )
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('your app is listening on port ' + listener.address().port)
});
My .env variable:
MONGO_URI="mongodb+srv://romif:<password>@cluster0.k1h7bvo.mongodb.net/exercice_tracker-db?retryWrites=true&w=majority"
My JSON file:
{
"name": "code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Romifff",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongodb": "^5.7.0",
"mongoose": "^7.4.0"
}
}
My browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 OPR/99.0.0.0
Challenge: Back End Development and APIs Projects - Exercise Tracker
Link to the challenge: