product validation failed error while using post api

I have created post and get request for product in node express. get request API is working fine but post request through an error:

models.js file

const mongoose=require("mongoose")

const produdctSchema=new  mongoose.Schema({
    name: {
        type: String,
        required:true,
        trim: true,
      },
    description:{
        type:String,
        required:[true, "please enter product description"]
    },
    price:{
        type:Number,
        required:[true, "please enter product price"]
    },
    rating:{
        type:Number,
        default:0
    },

    category:{
       type:String,
        required:true
    },
    stock:{
        type:Number,
        requird:true,
        default:1
    },
    numOfReview:{
        type:Number,
        default:0,

    },
   
   createdAt:{
     type:Date,
     default:Date.now()
 }
})

module.exports=mongoose.model("product",produdctSchema)

routes.js file

const express = require("express");
const {allproductController,createProductController}=require("../controllers/productControllers.js")

const router=express.Router();

router.route("/product/new").post(createProductController);
router.route("/products").get(allproductController)

module.exports= router

controllers.js file

const Product= require("../models/productModel.js")

exports.createProductController=async (req,res,next)=>{
    try {
   
        const product = await Product.create(req.body);
        res.status(201).json({
            success:true,
            product
        })
    } catch (error) {
        res.status(500).json({
            success:false,
            message:error.message
        })
    }
};

exports.allproductController=async(req,res)=>{
    try {
        const products=await Product.find();
        res.status(200).json({message:"success",data:products})
    } catch (error) {
        res.status(500).json({message:"error"})
    }

}

get API is working but when post API call it through an catch error in post man
"product validation failed: category: Path category is required., price: please enter product price, description: please enter product description, name: Path name is required." }



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations