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

370
Vistas
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
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