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

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

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 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