'How to add reference id in mongodb when we upload csv file data in mongodb
convertCsv()
.fromFile(`uploads/csv_Employee/${req.body.file}`)
.then((csvData) => {
const obj = { createdById: req.body.createdById };
csvData.obj;
const finalData = Employee.insertMany(csvData);
if (finalData) {
return res.json(
Response(
constants.statusCode.ok,
constants.attendanceMsg.attendanceSuccess,
finalData
)
);
} else {
return res.json(
Response(
constants.statusCode.unauth,
constants.messages.internalServerError
)
);
}
});
Solution 1:[1]
Try to loop through the csvData array and add the createdById property to each element before populating the table.
Also, you may want to await the insertMany operation since it returns a Promise:
.then(async (csvData) => {
csvData.forEach(data => {
data.createdById = req.body.createdById
})
const finalData = await Employee.insertMany(csvData);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lpizzinidev |
