{
"product": [
{
"product_ID": "test",
"productDetail": "test",
"income_ID": "6297343ec4cc7b1ca85521ba"
},
{
"product_ID": "test",
"productDetail": "test",
"income_ID": "6297343ec4cc7b1ca85521ba"
}
]
}
this my data array I used axios send data to express and mongodb but it's not working
and this my express code to save data in mongodb
quotationRoute.route('/incomeProduct').post((req, res, next) => {
var newProduct = new Qot({
product:product.req.body.product,
})
newProduct.save(err => {
if (err) {
return res.status(400).json({
title: 'error',
error: 'error'
})
}
return res.status(200).json({
title: 'signup success',
id: newProduct._id
})
})
If your schema is the following:
{
productDetail: "String",
productQyt: "String",
productPrice: "String",
productUnit: "String",
createdAt: {
type: "Date",
default: "Date.now",
},
income_ID: {
type: "mongoose.Schema.Types.ObjectId",
ref: "Business",
},
product_ID: "String",
};
And the data you are sending to your server is:
{
"product": [
{
"product_ID": "test",
"productDetail": "test",
"income_ID": "6297343ec4cc7b1ca85521ba"
},
{
"product_ID": "test",
"productDetail": "test",
"income_ID": "6297343ec4cc7b1ca85521ba"
}
]
}
You have an array of objects so it looks like you want to save multiple quotations for that purpose you could use the insertMany methods of mongoose
here is an example of how your code needs to be:
quotationRoute.route('/incomeProduct').post(async (req, res, next) => {
try {
await Qot.insertMany(req.body.product);
return res.status(200).json({
title: 'signup success',
// id: newProduct._id as you save multiple Qot this will be unecessary
});
} catch (e) {
return res.status(400).json({
title: 'error',
error: 'error',
});
}
});