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

240
Vistas
Nextjs MongoDB template: clientPromise is not a function

I am using Next.js's MongoDB example template and it comes with a MongoDB util function:

import { MongoClient } from 'mongodb';

const uri = process.env.MONGODB_URI;
const options = {};

let client;
let clientPromise;

if (!process.env.MONGODB_URI) {
  throw new Error('Please add your Mongo URI to .env.local');
}

if (process.env.NODE_ENV === 'development') {
  if (!global._mongoClientPromise) {
    client = new MongoClient(uri, options);
    global._mongoClientPromise = client.connect();
  }
  clientPromise = global._mongoClientPromise;
} else {
  client = new MongoClient(uri, options);
  clientPromise = client.connect();
}
export default clientPromise;

Here is how I am using it and I am certain that I am using this incorrectly.

import { clientPromise } from '../../lib/mongodb';

export default async (req, res) => {
  try {
    const db = await clientPromise();
    const users = await db.collection('users').find({}).limit(20).toArray();
    res.json(users);
  } catch (e) {
    console.error(e);
    res.json({ error: 'Not connected!' });
  }
};

The error is "TypeError: (0 , lib_mongodb__WEBPACK_IMPORTED_MODULE_0_.clientPromise) is not a function"

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

0

You're exporting clientPromise, which is client.connect(). At this point you already "triggered" the connection function. now all you have to do is wait on that promise,

So instead of

const db = await clientPromise();

You should do:

const connection = await clientPromise; // this is a client connection not a db
const db = connection.db();
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