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

369
Vistas
MongooseError: `Model.findByIdAndUpdate()` cannot run without a model as `this`. Make sure you are not calling `new Model.findByIdAndUpdate()`

This is how I connected to the database

const dbConnection = async() => {
try {
    await mongoose.connect(process.env.MONGODB_CNN);
    console.log('Base de datos online');
} catch (error) {
    console.log(error);
    throw new Error('Error al inicializar la base de datos');
}

the problem comes when updating

const usuariosPut = (req = request, res = response) => {
const {id} = req.params;
const {password, google, correo, ...resto} = req.body;
if (password) {
    const salt = bcryptjs.genSaltSync();
    resto.password = bcryptjs.hashSync(password, salt);
}
const usuario = new Usuario.findByIdAndUpdate(id, resto);
res.json({
    msg: 'put API - controlador',
    id
});

I get that error, it would be very helpful since I just started

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Because you are trying to create a new instance of the model model rather than directly calling the method on the model.

const usuario = new Usuario.findByIdAndUpdate(id, resto);

should be:

const usuario = await Usuario.findByIdAndUpdate(id, resto);

Additionally, I don't see any async keyword or Promise.then() in your code. Model methods are asyncronous, so make sure to mark your main function async like this:

const usuariosPut = async (req = request, res = response) => {
about 4 years ago · Juan Pablo Isaza Denunciar

0

Did you created Usuario model before? If so, you do not need to call model constructor with new keyword, your code should look like this:

const usuario = await Usuario.findByIdAndUpdate(id, resto);
about 4 years ago · Juan Pablo Isaza 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