Ismail Chaudry
Ismail Chaudry

Reputation: 1

Mongoose 'uri' parameter not connecting?

Guys I am decently new to backend and I have been trying to run my application but it just doesn't work, It was working at first but I tweaked it somewhere? could someone this knowledge help?

this is the error Error connecting to MongoDB: 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.

this is my server.js code (pls let me know if I need to add more):

// server.js

require('dotenv').config();

const express = require('express');

const app = express();

const cors = require('cors');

const PORT = process.env.PORT || 5001 ;

const blogRoutes = require('./routers/blogRoutes');  // Import your blogRoutes
const mongoose = require('mongoose');


// Import the Blog model (from models/Blog.js)

const Blog = require('./models/Blog');  // Make sure this is the correct path

app.use(cors());

app.use(express.json());

mongoose.connect(process.env.MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
})
.then(() => {
    console.log('Connected to MongoDB');
})
.catch((error) => {
    console.error('Error connecting to MongoDB:', error);
});

app.use('/blogs', blogRoutes);  // Use the routes for handling blog posts

app.listen(PORT, () => {
    console.log(`Server is successfully running on port ${PORT}`);
});

I think their is something wrong with my uri in my env but it is perfectly find and good?

Upvotes: -1

Views: 19

Answers (0)

Related Questions