I'm trying to send data and upload an image to the backend (Node.js) in the same request, but when I try to upload the image, the other properties are found as undefined on the backend.
const Imgdata = new FormData()
Imgdata.append('file', this.state.choiceImgUrl)
axios.post("http://localhost:8080/api/choices/img", Imgdata, {
headers: { 'Content-type': 'application/json' },
data:{
id:this.state.id,
username:this.state.username
}
})
.then(res => {
if(res.data.success){
this.setState({ successUpload:true })
}
else {
alert('error in upload data')
this.setState({ Uploadloading:false })
}
})
The id
and username
properties are undefined in the backend, and if I remove Imgdata
, All data is transferred successfully, but without the file.
How can I upload the file along with some data to the backend?