I am creating an E-commerce project and I am in a dilemma creating an Orders Schema. According to me, I am convinced that a customer cannot sometimes order one item and therefore the orders should be in an array, but I am having trouble navigating through this as I am a beginner. this is what I have come up with within my model and controller but whenever I post data the res.json that I receive comes with an empty object.
I hope someone can help with this.
My Order model
import mongoose from 'mongoose'
const ordersSchema = new mongoose.Schema({
user:{
type:mongoose.Schema.Types.ObjectId,
required:true,
ref:"users",
},
orders:[
{
item:{
type:mongoose.Schema.Types.ObjectId,
required:true,
ref:'products'
},
quatity:{
type:Number,
required:true,
default:1
},
price:{
type:Number,
required:true,
}
}
]
});
export const ordersModel = mongoose.model("orders", ordersSchema);
My controller code for creating an order
import { ordersModel} from "../models/orders.model.js"
import asyncHandler from 'express-async-handler'
export const makeOrder =asyncHandler(async(req, res) => {
const {item, price, quantity, payment} = req.body;
if(!item || !price ){
res.sendStatus(401)
res.json({message: 'There is no order to make'});
}else{
const order = new ordersModel(
{
user:req.user._id,
order:[
{
item,
price,
quantity,
payment
}
],
}
);
const made_order = await (order.save());
if(made_order){
return res.json({
orderId:made_order._id,
customer:made_order.user._id,
order:{
item:made_order.item,
price:made_order.price,
quantity:made_order.quantity,
payment:made_order.payment,
}
})
}
}
});
CodePudding user response:
I can see a typo when creating a new order. You have orders field in schema but you are passing order without s at the end when creating a new order. Below is the correct implementation
const order = new ordersModel(
{
user: req.user._id,
orders: [
{
item,
price,
quantity,
payment
}
],
}
);
You also don't have payment field in schema but you are passing it when creating order. So your schema should be this if you want to include payment field
const ordersSchema = new mongoose.Schema({
user:{
type:mongoose.Schema.Types.ObjectId,
required:true,
ref:"users",
},
orders:[
{
item:{
type:mongoose.Schema.Types.ObjectId,
required:true,
ref:'products'
},
quatity:{
type:Number,
required:true,
default:1
},
price:{
type:Number,
required:true,
},
payment:{
type:Number,
required:true,
}
}
]
});
In the end you can remove the if statement and just send a res like this
const made_order = await order.save();
return res.json({
orderId:made_order._id,
customer:made_order.user,
orders: made_order.orders
})
If you want to create multiple orders in a single call, your makeOrder controller should be like this
export const makeOrder = asyncHandler(async(req, res) => {
if(!Array.isArray(req.body) || !req.body.length) {
res.sendStatus(401)
res.json({message: 'There is no order to make'});
} else {
const order = new ordersModel(
{
user: req.user._id,
orders: req.body,
}
);
const made_order = await order.save();
return res.json({
orderId: made_order._id,
customer: made_order.user,
orders: made_order.orders
})
}
});
Then you can make an API call and send multiple orders as req body
[
{
"item":"61f58bb0d70ae52c6e470fb7",
"price":4500,
"quantity":2,
"payment": 9000
},
{
"item":"61f58bb0d70ae52c6e470fb7",
"price":4500,
"quantity":2,
"payment": 9000
}
]
