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

248
Vistas
How to connect to mongoDB from typescript

I am getting an error when trying to add the mongo connection to the request

Property 'dbClient' does not exist on type 'Request<ParamsDictionary>'.

I would like to have the connection available without having to add it to all my routes.

Thanks for your help

Here is my code snippet

const mongoUri = process.env.MONGO_URI ? process.env.MONGO_URI : "";
const mongoClient = new MongoClient(mongoUri);
async function createMongoConnecion() {
  try {
    await mongoClient.connect();
    console.log("connected");
  } catch (err) {
    console.log("Error connecting to mongo: " + err);
  } finally {
    console.log("Connected to mongo successfully!");
  }
}

app.use(async function (req, res, next) {
  req.dbClient = mongoClient;
  next();
});

routes.push(new SearchRoutes(app));



over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Step 1:

In the root folder, create the file types/express/index.d.ts with this content:

 declare global { namespace Express { interface Request { dbClient: object; } } }

Step 2

Update the tsconfig.json file:

 { "compilerOptions": { "typeRoots": ["./types"] } }
over 4 years ago · Santiago Trujillo 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