Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Mogodb refuses to connect after changing the app directory

I had my App (mern stack, but this is just about the backend) running inside another directory where i had both frontend and backend running in the same folder. Today i splitted them up and put the backend stuff inside another folder to keep backend and frontend in different folders. After running yarn start:backend it does connect, and i get my message "server started at http://localhost:5000". However, instead of getting "mongoose connected", i get "undefined" and the routes doesent work.

I thought it might be because it does not find the env variables, but after inserting the if statement it shows the route to my db, so this cant be the reason.

server.mjs

import express from 'express';
import dotenv from 'dotenv';
import config from './config.mjs'
import mongoose  from 'mongoose';
import userRoute from './routes/userRoute.mjs';
import productRoute from './routes/productRoute.mjs';
import orderRouter from './routes/orderRouter.js';
import bodyParser from 'body-parser';
import cors from 'cors';

//load env context into dotenv
dotenv.config();

//middleware
const app=express();
app.use(cors());                              
app.use(bodyParser.json());                   
app.use(express.json());                       
app.use(express.urlencoded({extended: true}));

// error middleware
app.use((err, req, res, next)=> {
    res.status(500).send({ message: err.message});
})

if(config.mongodb_URL){console.log(config.mongodb_URL);}
else console.log("not found")


// MONGODB CONNECTION + Test
const mongodbUrl = config.mongodb_URL;
mongoose.connect(mongodbUrl,{
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true
}).catch(error=>console.log(error.reason));

mongoose.connection.on('connected', ()=> {
    console.log('mongoose connected')
});




    //concat link
app.use("/api/users", userRoute);
app.use("/api/products", productRoute);
app.use("/api/orders", orderRouter);
app.get('/api/config/paypal', (req, res) => {
    res.send(process.env.PAYPAL_CLIENT_ID || 'sb');
})



app.listen(5000, ()=> {console.log("server started at http://localhost:5000")}); 

config.mjs

import dotenv from 'dotenv';
dotenv.config();

export default {
    mongodb_URL: process.env.MONGODB_URL || "",
    JWT_SECRET: process.env.JWT_SECRET || 'somethingsecret'
}

I really dont have a clue where i can search, because i dont have any errors inside the browser and no errors inside vscode.

What have i forget and what do you think where the error might be?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As i found out:

Delete following Line: useCreateIndex: true

There is a new Mongodb version and it takes useCreateIndex as true by default, so this line has to be deleted. Afterwards it works!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!