[英]why I got this type error not a constructor for javascipt?
I am trying to create a new product.to test whether the result is sent to my mongodb, I input the values in postman.However, I am confused about this error.我正在尝试创建一个新产品。为了测试结果是否发送到我的 mongodb,我在 postman 中输入了值。但是,我对这个错误感到困惑。
This is my Product file这是我的产品文件
const mongoose = require("mongoose");
const ProductSchema = new mongoose.Schema(
{
title: {type:String , required:true , unique:true},
description: {type:String , required:true},
img: {type:String , required:true },
categories: {type: Array , required:true },
size: {type:Array},
color: {type:Array},
price: {type: Number , required:true },
inStock:{type:Boolean , default:true},
},
{ timestamps: true }
);
module.export = mongoose.model("Product" , ProductSchema);
This is my post code这是我的邮政编码
const router = require("express").Router();
const Product = require("../model/Product")
//Create new product(testing in postman)
router.post("/", async (req,res)=>{
const newProduct = new Product(req.body);
try{
const savedProduct = await newProduct.save();
res.status(200).json(savedProduct);
}catch(error){
res.status(500).json(error)
}
})
After I send the values in postman, I always encounter this error.在我发送 postman 中的值后,我总是遇到这个错误。
TypeError: Product is not a constructor
You are using您正在使用
module.export = mongoose.model("Product" , ProductSchema);
it should be它应该是
module.exports = mongoose.model("Product" , ProductSchema);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.