Currently I am able to post this to mongodb. It works fine.
PROBLEM Instead of one attachment, I should be able to post many attachments independent of each other, there will be N different buttons available for the N uploads.
const form = (req, res, next) => {
const file = req.files.photo;
file.name = `photo_${Math.random(0, 10012)}-${Math.random(0, 2000)}${
path.parse(file.name).ext
}`;
file.mv(`./public/uploads/${file.name}`, async (err) => {
if (err) {
console.error(err);
return res.status(500).json({
message: `Problem With File Upload`,
});
}
const upload = await Form.create({
approved: req.body.approved,
email_: req.body.email,
formData: {
name: "req.body.formData.claimantName",
nationality: "req.body.formData.claimantNationality",
address: "req.body.formData.claimantAddress",
email: "req.body.formData.claimantEmail",
},
fileOne: file.name1,
// these are the next
// fileTwo: req.body.formData.name2,
// fileThree: req.body.formData.name3,
});
return res.status(200).json({
success: true,
message: `File Uploaded Successfully`,
path: file.name,
});
});
};
router.route("/add").post(form);
I tried moving const upload = await Form.create(...)
outside the file.mv(...)
block and do somthing like this```
const file1 = req.files.photo1;
file1.name = photo_${Math.random(0, 10012)}-${Math.random(0, 2000)}${ path.parse(file1.name).ext }
;
It doesn't work properly.