Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

643
Vistas
Getting Error: querySrv ECONNREFUSED _mongodb._tcp.auctiondbcluster.s6rzg.mongodb.net. When trying to connect to localhost when I am offline
MongoDB connected...
Development server live on http://localhost:3000
node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

Error: querySrv ECONNREFUSED _mongodb._tcp.auctiondbcluster.s6rzg.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (node:dns:213:19) {
  errno: undefined,
  code: 'ECONNREFUSED',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.auctiondbcluster.s6rzg.mongodb.net'
}

Everything works smooth when I am connected to my wifi but when I am offline and starting the server it gives this error. And by the way no I am not trying to connect to MongoDB online cluster, I am trying to connect to local host (mongodb://localhost:27017/auctionDB).

Here the function I am using in app.js

const runServer = async () =>{
  if(process.env.NODE_ENV !== "production"){
    await connectDb(process.env.MONGODB_LOCAL_CONNECTION);
    app.listen(PORT, ()=>{
      console.log(`Development server live on http://localhost:${PORT}`);
    })
  } else {
    await connectDb(process.env.MONGODB_CONNECTION_STRING);
    app.listen(PORT, ()=>{
      console.log(`Production server live on port ${PORT}`);
    })
  }
}

runServer();

In my .env file NODE_ENV is set to development. Here is the connectDb.js function I am using to connect to the database

const mongoose = require("mongoose");

/**
 * @description: Connect to the database by providing the connection string.
 * @param {String} uri | MongoDB URI
 * @default: mongodb://localhost:27017/auctionDB
 *
 * @returns {undefined}
 */
const connectDb = (url = process.env.MONGODB_LOCAL_CONNECTION) => {
    try {
      const con = mongoose.connect(url, {
        useNewUrlParser: true,
        useUnifiedTopology: true
      });
      if(con) console.log("MongoDB connected...")
    } catch(e){
      console.log(`Error: ${e}`);
    }
};  

module.exports = connectDb;

Here is the entire project on github

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should await the mongoose.connect call, not the whole function:

const connectDb = async (url = process.env.MONGODB_LOCAL_CONNECTION) => {
    try {
      const con = await mongoose.connect(url, {
        useNewUrlParser: true,
        useUnifiedTopology: true
      });
      if(con) console.log("MongoDB connected...")
    } catch(e){
      console.log(`Error: ${e}`);
    }
};  

module.exports = connectDb;
const runServer = () =>{
  if(process.env.NODE_ENV !== "production"){
    connectDb(process.env.MONGODB_LOCAL_CONNECTION);
    app.listen(PORT, ()=>{
      console.log(`Development server live on http://localhost:${PORT}`);
    })
  } else {
    connectDb(process.env.MONGODB_CONNECTION_STRING);
    app.listen(PORT, ()=>{
      console.log(`Production server live on port ${PORT}`);
    })
  }
}

runServer();
about 4 years ago · Juan Pablo Isaza Denunciar

0

Make sure your MongoDB URL is [ mongodb://localhost:27017/ ], and not an online connection MongoDB URL. At line 13

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda