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

358
Views
mongoose UnCaught TypeError: foundProduct.greet is not a function

So the title says it all. Seems like for some reason I can't access the greet function, and I really don't understand why. I'm following a teacher froma course and everything seems to be working fine on is side this is the .js :

    const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/shopApp")

    .then(() => {  //check if you succesfully connect to mongodb
        console.log("Connection Open !")
    })
    .catch(err => {
        console.log("Oh No Error")
        console.log(err)
    })

const productSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        maxLength: 20
    },
    price: {
        type: Number,
        required: true,
        min: [0, "Price must be positive !"]
    },
    onSale: {
        type: Boolean,
        default: false
    },
    categories: [String],
    qty: {
        online: {
            type: Number,
            default: 0
        },
        inStore: {
            type: Number,
            default: 0
        }
    },
    size: {
        type: String,
        enum: ["S", "M", "L"]//these are the only string that'll be accepted 
    }
})


const Product = mongoose.model("Product", productSchema)

productSchema.methods.greet = function () {
    console.log("hello ! ")
    console.log(`- from ${this.name}`)
}


const findProduct = async () => {
    const foundProduct = await Product.findOne({ name: "Tire pump" });
    foundProduct.greet();
}
findProduct()

I know the problem is from const Product and below but cant find anything wrong

and here is the reponse a get when I .load product.js with node :

Uncaught TypeError: foundProduct.greet is not a function

about 4 years ago · Juan Pablo Isaza
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!