Recibo un error al intentar agregar la conexión mongo a la solicitud
La propiedad 'dbClient' no existe en el tipo 'Request<ParamsDictionary>'.
Me gustaría tener disponible la conexión sin tener que añadirla a todas mis rutas.
Gracias por tu ayuda
Aquí está mi fragmento de código
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));
En la carpeta raíz, cree el archivo types/express/index.d.ts
con este contenido:
declare global { namespace Express { interface Request { dbClient: object; } } }
Actualice el archivo tsconfig.json
:
{ "compilerOptions": { "typeRoots": ["./types"] } }