please help me to resolve this issue in my app. here is the object, we have motherPhoto, fatherPhoto,spousePhoto and siblingsPhoto to be uploaded..
{mothername:'kerin', motherPhoto:"C:\\fakepath\\mother.jpg" ,fathername:'kako', fatherPhoto:"C:\\fakepath\\father.jpg",spousename:'kiki', spousePhoto:"C:\\fakepath\\wife.jpg",siblingDetails:'[{"siblingsName":"","siblingsDob":"","siblingsEmail":"","siblingsPhone":"","siblingsPhoto":"C:\\fakepath\\LW.jpg"},{"siblingsName":"","siblingsDob":"","siblingsEmail":"","siblingsPhone":"","siblingsPhoto":"C:\\fakepath\\LW1.jpg"},{"siblingsName":"","siblingsDob":"","siblingsEmail":"","siblingsPhone":"","siblingsPhoto":"C:\\fakepath\\LW3.jpg"},{"siblingsName":"","siblingsDob":"","siblingsEmail":"","siblingsPhone":"","siblingsPhoto":"C:\\fakepath\\LW4.jpg"},]'}
I’m able to get, motherPhoto, fatherPhoto,spousePhoto but siblingsPhoto is {} empty object
in react,
const handleSubmit = async (event: any) => {
event.preventDefault()
setValidated(true)
let error = hasError(formData)
if (isEmpty(error)) {
let data = new FormData()
Object.keys(formData).forEach((key) => {
if (key === 'siblingsDetails') {
if (formData.isSiblings === 'No') {
data.append('siblingsDetails', JSON.stringify([]))
} else {
console.log(formData[key], 'formData[key]')
data.append(key, JSON.stringify(formData[key]))
}
} else {
data.append(key, formData[key])
}
})
await applyCareer(data)
} else {
const a = Object.keys(error)
event.target.querySelector('#' + a[0]).focus()
setErrors(error)
}
}
const applyCareer = async (data: any) => {
try {
await axios.post(
`${import.meta.env.VITE_API_URL}job-applications/add/personalInfo`,
data,
{
headers: {
applicant: 'hello',
'Content-Type': 'multipart/form-data',
},
}
)
} catch (error) {
throw error
}
}
in nodejs
import multer from "multer";
const router = express.Router();
const storage = multer.diskStorage({
destination: function (req, file, cb) {
if (file.fieldname === 'motherPhoto') {
cb(null, 'uploads/careers/personalInfo/motherPhoto/');
}else if (file.fieldname === 'fatherPhoto') {
cb(null, 'uploads/careers/personalInfo/fatherPhoto/');
}else if (file.fieldname === 'spousePhoto') {
cb(null, 'uploads/careers/personalInfo/spousePhoto/');
}else if (file.fieldname === 'siblingsPhoto') {
cb(null, 'uploads/careers/personalInfo/siblingsPhoto/');
}
},
filename: function (req, file, cb) {
const userId = req.headers.applicant;
let filename = '';
if (file.fieldname === 'fatherPhoto') {
filename = `${userId}_Father_${Date.now()}.${file.mimetype.split('/')[1]}`;
} else
if (file.fieldname === 'motherPhoto') {
filename = `${userId}_mother_${Date.now()}.${file.mimetype.split('/')[1]}`;
} else
if (file.fieldname === 'spousePhoto') {
filename = `${userId}_spouse_${Date.now()}.${file.mimetype.split('/')[1]}`;
}else
if (file.fieldname === 'siblingsPhoto') {
filename = `${userId}_sibling_${Date.now()}${path.extname(file.originalname)}`;
}
cb(null, filename);
},
});
const upload = multer({ storage: storage });
router.post("/add/personalInfo", upload.fields([
{ name: 'passportImage', maxCount: 1 },
{ name: 'fatherPhoto', maxCount: 1 },
{ name: 'motherPhoto', maxCount: 1 },
{ name: 'spousePhoto', maxCount: 1 },
{ name: 'siblingsPhoto', maxCount: 10 } // Allow up to 10 sibling photos
]),async(req,res)=>{
try {
console.log(req.files,'files')
} catch (error) {
res.status(500).send(error);
}
})
Issue:
The siblingsPhoto array is not being populated correctly on the server side.
In the React code, siblingsPhoto is an empty object when I check the req.files on the server.
Question:
How can I correctly handle and upload multiple siblingsPhoto files along with other individual photo fields in this setup? Any help or suggestions would be greatly appreciated!Issue:
The siblingsPhoto array is not being populated correctly on the server side.
In the React code, siblingsPhoto is an empty object when I check the req.files on the server.
Question:
How can I correctly handle and upload multiple siblingsPhoto files along with other individual photo fields in this setup? Any help or suggestions would be greatly appreciated!