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.
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.
TypeError: Product is not a constructor
You are using
module.export = mongoose.model("Product" , ProductSchema);
it should be
module.exports = mongoose.model("Product" , ProductSchema);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.