Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda