I need to post multiple array at the same time so i can achieve this :
{
name:"John Snow",
detail: [
{size: "M", total: 5, color: "red"},
{size: "S", total: 2, color: "black"}
]
}
i'm using dynamic form that can generate new input field for detail array. so this is my schema model :
const OrderSchema = new Schema({
name:{
type: String,
required: true
},
detail:[{
size:{},
color:{},
total:{}
}],
date:{
type: Date,
default: Date.now
}
});
this is my route post :
router.post('/add', (req, res) => {
let errors = [];
if (!req.body.name) {
errors.push({ text: 'Your customer name is empty' });
}
if (errors.length > 0) {
res.render('orders/add', {
errors: errors,
customer: req.body.name,
detail:[{
size: req.body.size,
color: req.body.color,
total: req.body.total
}]
});
} else {
const newUser = {
customer: req.body.name,
detail:[{
size: req.body.size,
color: req.body.color,
total: req.body.total
}
new Order(newUser)
.save()
.then(order => {
res.redirect('/');
})
}
});
submit post is work, it just the result is not what i want :
{
name:"John Snow",
detail: [
{size: ["M","S"], total: [5,2], color: ["red", "black"]}
]
}
i hope you can help me, thanks!