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

440
Vistas
Cannot solve the 'Model cannot be overwritten once complied' error on mongoose

Problem: So I'm trying to integrate MongoDB into my nextJs project. Here's a library file which connects to the database.

import shopSchema from './Schemas/shopSchema';
let shopModelCached;
async function getShopModel(connectionUrl) {
  if (!shopModelCached) {
    try {
      await mongoose.connect(process.env.MONGO_DB_CONNECTION_STRING);
      const Shop = mongoose.model("shop", shopSchema);
      shopModelCached = Shop;
    } catch (err) {
      throw err;
    }
  }
  return shopModelCached;
}

export default getShopModel;

When I call the function to get the shop model on my getServerSidedProps on my page, which does literally nothing but call the above function to get the Shop model, and then performs simple query to populate the props.

export async function getServerSideProps(context) {
  try {
    const Shop = await getShopModel(
      process.env.MONGO_DB_CONNECTION_STRING
    );
    const shop = await Shop.findOne({
      shopcode: context.params.shopname,
    }).exec();
    return {
      props: {
        shop,
      },
    };
  } catch (err) {
    throw err;
  }
}

This hits me with an error when I recompile modules. It says, 'OverwriteModelError: Cannot overwrite 'shop' model once compiled.'. My approach to handling this was to cache the model so it wouldn't be overwritten, but to no vain.

about 4 years ago · Juan Pablo Isaza
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