I am inserting data from CSV file, I want to update if the object key has the same then the new price will just update, I have used csvtojson for parsing CSV files and multer for file upload.
CSV file has itemNumber, title, and price column
sample.csv
itemNumber,title,price
STY0047,Men Straight Fit Jeans,2500
STY0048,Men Straight Fit Jeans,2450
STY0049,Men Straight Fit Jeans,2550
STY0050,Men Straight Fit Jeans,3000
STY0048,Men Straight Fit Jeans,4000
STY002,Men Straight Fit Jeans,2550
STY0053,Men Straight Fit Jeans,3000
STY0054,Men Straight Fit Jeans,3000
STY0043,Men Straight Fit Jeans,4000
My code
router.post("/file-insert-data", upload, async function (req, res, next) {
if (!req.file) {
return res.status(400).send("no file found");
}
const results = await csv().fromFile(req.file.path);
for (let obj in results ) {
const csvData = new csvModel(results [obj])
csvData.save((err, data) => {
if(err) {
return res.status(400).send("csv data is not inserted")
}
return res.status(201).send(data)
})
}
});
Help me Thanks