I'm getting some troubles passing an groupId
via post to my function to process it, basically I'm uploading a picture but I need to pass groupId to rename it picture on my other function, I'm using react and express to do this:
onFormSubmit = async (event) => {
event.preventDefault();
const { imageFile, currentGroupId } = this.state;
var formData = new FormData();
formData.append('groupImage', imageFile);
const config = {
headers: {
'content-type': 'multipart/form-data'
}
};
await api.post('/fileUpload/group', { currentGroupId }, formData, config );
}
My route:
router.post('/group', (req, res) => {
fileuploadmanager.uploadFile(req, res);
});
And the backend side:
uploadFile(req, res) {
const { uploadsPath } = this.porperties;
const { currentGroupId } = req.body;//it prints but formData and config never comes here
console.log("GROUP: ", currentGroupId);
const storage = multer.diskStorage({
destination: uploadsPath,
filename: (req, file, cb) => {
cb(null, `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`);
},
});
const upload = multer({
storage,
limits: {
fileSize: 1000000,
},
fileFilter: (req, file, cb) => {
this.checkFileType(file, cb);
},
}).single('groupImage');
upload(req, res, (err) => {
if (err) {
console.log(err);
}
});
}
Sending just formData
and config
works but it doesn't work if I add groupId to the endpoint.
I tried appending it value in formData
but I can't get it on the backend side, so now I trying passing it as a req.body, it works fine but my other paramters is not passing to the backend.
How can I pass my groupId, formData and config to my backend side?
EDIT: api
is a wrapper to specify my backend URL. I'm using axios
import axios from 'axios';
export default axios.create({
baseURL: process.env.REACT_APP_API_URL
});
References: