Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

366
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!