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

360
Visualizações
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 à 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