I'm trying to send data with a file from my React post to NodeJS.
In React I have a form with some inputs of type string and number and an input of type File
<Input type="file" name="file" id="file"
innerRef={register()}
/>
I send the form submit data to NodeJS using axios: The data is an object with my data:
const data = {
amount: data.debitAmt,
invoiceDate: data.invoiceDate,
memo: data.memo,
file: data.file
};
await axios.post(`${url.REQ_URL}/bills/add`, {
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
data
});
When I check the NodeJS, the file is an empty object:
exports.add = async (req, res, next) => {
const data = req.body.data || req.body;
console.log(data);
}
The log shows:
{
amount: '10',
invoiceDate: '2021-04-02',
memo: '',
file: { '0': {} }
}
Why it is not sending the file? Do I need a separated axios.post just for the file?
Thanks