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