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

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

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 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!