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

208
Vistas
Cache MongoDb connection with Next.js 10 TypeScript Project - API Route

I'm trying to convert next.js/examples/with-mongodb/util/mongodb.js to TS so I can cache and resue my connections to Mongo within a TS next.js project. I'm getting a TS error on cache.promise that says: Type 'Promise<MongoClient | { client: MongoClient; db: Db; }>' is not assignable to type 'Promise<MongoClient>'

How should I properly declare the mongo property on global to appease the TS gods?

import { MongoClient, Db } from "mongodb";

const { DATABASE_URL, DATABASE_NAME } = process.env;

declare global {
  namespace NodeJS {
    interface Global {
      mongo: {
        conn: MongoClient | null;
        promise: Promise<MongoClient> | null;
      };
    }
  }
}

let cached = global.mongo;

if (!cached) {
  cached = global.mongo = { conn: null, promise: null };
}

async function connect() {
  if (cached.conn) {
    return cached.conn;
  }

  if (!cached.promise) {
    const opts = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    };

    cached.promise = MongoClient.connect(DATABASE_URL, opts).then((client) => {
      return {
        client,
        db: client.db(DATABASE_NAME),
      };
    });
  }
  cached.conn = await cached.promise;
  return cached.conn;
}

export { connect };
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You don't need to cache your connection, check latest nextjs with mongodb example. The official mongodb forum experts have navigated me to this example project.

Try to use native solutions

over 4 years ago · Santiago Trujillo Denunciar

0

The 'conn' property you are storing contains both MongoClient and Db.

In your global declaration for mongo, you have only included MongoClient. I have the exact same code in my project and the way I handle this is to simply create a basic type called MongoConnection which contains both. Code below.

type MongoConnection = {
  client: MongoClient;
  db: Db;
};

declare global {
  namespace NodeJS {
    interface Global {
      mongo: {
        conn: MongoConnection | null;
        promise: Promise<MongoConnection> | null;
      }
    }
  }
}
over 4 years ago · Santiago Trujillo Denunciar

0

seems like the answer is to just make the mongo property an any like this:

declare global {
  namespace NodeJS {
    interface Global {
      mongo: any;
    }
  }
}
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