所以标题说明了一切。似乎出于某种原因我不能访问问候功能,我真的不明白为什么。我正在跟随一位老师从课程和一切似乎是工作正常的一面,这是.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()
我知道问题是从const产品和以下,但找不到任何错误
下面是当我用node加载product.js时得到的响应:
未捕获的类型错误:foundProduct.greet不是函数
1条答案
按热度按时间vqlkdk9b1#
你需要声明上面的示例方法
const产品= Mongoose 模型(“产品”,产品架构)