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

217
Vistas
Mongose method is not a function

I am using mongoose and every time I try to use a method that I created for a schema, it throws the following error message:

UnhandledPromiseRejectionWarning: TypeError: findItem.updatePrice is not a function

Model definition and instance creation:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/shopApp')
.then(()=>{
    console.log('Working!');
})
.catch(err=>{
    console.log('Error');
    console.log(err);
})

const productSchema = new mongoose.Schema({
 name: {
     type: String,
     required: true
 },
 price: {
     type: Number
 }
}) 
const product = mongoose.model('product', productSchema);
const bike = new product({name:'MTB', price: 599});

productSchema.methods.updatePrice = function(newprice){
    this.price = newprice;
    return this.save();
}

const updatedPrice = async ()=>{
    const findItem = await product.findOne({name:'MTB'});
    await findItem.updatePrice(500);
}
updatedPrice();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The example in the docs defines the instance methods before creating an instance of it.

productSchema.methods.updatePrice = function(newprice){
    this.price = newprice;
    return this.save();
}

const product = mongoose.model('product', productSchema);
const bike = new product({name:'MTB', price: 599});
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