Having an issue with a MongoDB collection named 'sold' that contains both string and numeric fields.
const soldSchema = new Schema(
{
sku: String,
hsn: String,
qty: Number,
article: String,
mrp: Number,
disc: Number,
taxrate: Number,
tax: Number,
total: Number,
orderid: String,
},
{ collection: "sold", timestamps: true }
);
const Sold = mongoose.model("sold", soldSchema);
module.exports = { Customer, Stock, Sold, Order };
'Customer', 'Stock', and 'Order' are also defined within the same module.
Data for the sold schema is received from the frontend as an array of objects:
[
{
sku: '10005',
hsn: '3652',
qty: '3',
article: 'tops',
mrp: '550',
disc: '0',
taxrate: '5',
tax: '82.50',
total: '1732.50',
orderid: '1633515982545'
},
{
sku: '10005',
hsn: '3652',
qty: '3',
article: 'tops',
mrp: '550',
disc: '0',
taxrate: '5',
tax: '82.50',
total: '1732.50',
orderid: '1633515982545'
},
{
sku: '10005',
hsn: '3652',
qty: '3',
article: 'tops',
mrp: '550',
disc: '0',
taxrate: '5',
tax: '82.50',
total: '1732.50',
orderid: '1633515982545'
}
]
The above array represents req.body when logged using console.log(req.body).
When all fields in the soldSchema are set to type 'String', data gets inserted successfully in one go.
However, having issues when changing certain fields to Numbers in the schema. Seeking assistance on how to resolve this matter.
async function insertManySold(req, res) {
let items = req.body;
try {
let result = await model.Sold.insertMany(items);
res.json(result);
} catch (error) {
res.json(error);
}
}
Researching online has not provided a solution so far. Your help would be greatly appreciated.