I m using postman for post the data, sample is given below,after this it is giving error which i don't understand where i doing mistake,kindly some one help on this.
Error: order validation failed: Orditems.0.id: Path id is required., Orditems.0.dishName: Path dishName is required., Orditems.0.image: Path image is required., Orditems.0.price: Path price is required., Orditems.0.qty: Path qty is required., Orditems.0.total: Path total is required.
{
"name":"manoj sahni",
"mobile":12345678,
"ultermobile":21545544,
"address":"bhopal fsfadff dfgdsg",
"grandTotal":414,
"orderid":"64gd4gd",
"paymentid":"fghfh4f4",
"items":[
{
"id":1,
"dishName":"samaychiken",
"image":"muttonsaamey.jpg",
"price":200,
"qty":2,
"total":230
},
{
"id":2,
"dishName":"samaychiken",
"image":"muttonsaamey.jpg",
"price":210,
"qty":"1",
"total":210
}
]
}
Model
const mongoose=require("mongoose");
const ordSchema=mongoose.Schema({
Orditems:[{
id:{type:Number,required:true},
dishName:{type:String,required:true},
image:{type:String,required:true},
price:{type:Number,required:true},
qty:{type:Number,required:true},
total:{type:Number,required:true}
}],
name:{type:String,required:true},
mobile:{type:Number,required:true},
ultermobile:Number,
address:{type:String,required:true},
grandTotal:{type:Number,required:true},
orderid:{type:String},
paymentid:{type:String},
created_at:{type:Date,default:new Date()}
},{timestamps:true})
const ordr=new mongoose.model('order',ordSchema)
module.exports=ordr
Route
router.post('/invOrders',async(req,res)=>{
invData1=req.body
invData2=req.body.items
console.log("invorder data",invData1);
console.log("invorder data2",invData2)
try {
const ordr=new ordersModel(
{
Orditems:[{id:invData2.id,
dishName:invData2.dishName,
image:invData2.image,
price:invData2.price,
qty:invData2.qty,
total:invData2.total
}],
name:invData1.name,
mobile:invData1.mobile,
ultermobile:invData1.ultermobile,
address:invData1.address,
grandTotal:invData1.grandTotal,
orderid:invData1.orderid,
paymentid:invData1.paymentid,
}
)
const invSave= await ordr.save(err=>{
if(err){
console.log("inv errr",err)
}
},
succ=>{
if(succ){
console.log("invoice save ",succ)
}
})
} catch (error) {
console.log("catch error",error)
}
})
module.exports=router
CodePudding user response:
req.body.items
is an array, the problem is that you are trying to access it as an object.
Try to assign it directly to the Orditems
property when creating the new instance:
router.post('/invOrders', async (req, res) => {
invData1 = req.body;
invData2 = req.body.items;
try {
const ordr = new ordersModel({
Orditems: invData2,
name: invData1.name,
mobile: invData1.mobile,
ultermobile: invData1.ultermobile,
address: invData1.address,
grandTotal: invData1.grandTotal,
orderid: invData1.orderid,
paymentid: invData1.paymentid,
});
const invSave = await ordr.save(
(err) => {
if (err) {
console.log('inv errr', err);
}
},
(succ) => {
if (succ) {
console.log('invoice save ', succ);
}
}
);
} catch (error) {
console.log('catch error', error);
}
});
module.exports = router;