I try to test post request in thunder vscode to create new order but it gives me this error in screenshot.
I send post request in format json but the error is not resolved; when I remove required from ordermodel it works.
This is my node version: Node.js v18.12.1
This is the error (backticks removed for ease of reading):
{ "msg": "Order validation failed: shippingInfo.phoneNum: Path shippingInfo.phoneNum is required., shippingInfo.pincode: Path shippingInfo.pincode is required., shippingInfo.country: Path shippingInfo.country is required., shippingInfo.state: Path shippingInfo.state is required., shippingInfo.city: Path shippingInfo.city is required., shippingInfo.address: Path shippingInfo.address is required." }
This is the request body:
{
"shippingInfo": {
" address": "tunisia",
"city": "tunisia",
"state": "tunisia",
"country": "tunisia",
"pincode": 7100,
"phoneNum": 11558899
}
}
const mongoose = require("mongoose");
const { Schema } = mongoose;
const orderSchema = new mongoose.Schema({
shippingInfo: {
address: {type: String,required: true},
city: {
type: String,
required: true
},
state: {
type: String,
required: true
},
country: {
type: String,
required: true
},
pincode: {
type: Number,
required: true
},
phoneNum: {
type: Number,
required: true
},
},
});
const orderModel = mongoose.model("Order", orderSchema);
module.exports=orderModel
const orderModel = require("../models/orderModel");
//Create new order
;
const newOrder = async (req, res) => {
const {shippingInfo}=req.body
try {
const order= await new orderModel({shippingInfo})
// console.log(shippingInfo)
console.log(order)
await order.save()
res.status(201).send({ order: order, msg: "order added successfully" })
} catch (error) {
// console.log(error);
res.status(400).send({ msg: error.message });
}
};
module.exports = {newOrder };
const express = require("express");
const { newOrder } = require("../controllers/orderController");
const isAuth = require("../middlewares/isAuth");
const router = express.Router();
/**
* @Params POST /order/neworder
* @description create new order
* @acces protected
*/
router.post("/neworder",isAuth(),newOrder);
module.exports = router;