Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda