I am passing uploaded file and dataObject in FormData:
let data = new FormData();
data.append("file", this.state.files);
data.append("data", formData);
formData
is just a Json Object eg: formData = { "a":123 }
Passing data to the node server using put
request with axios
,I am able to get the file object and data in req, but in data field it's giving '[object Object]'
Don't know how to access this. I tried JSON.parse(req.body.data), but getting error
SyntaxError: Unexpected token o in JSON at position 1
I am passing uploaded file and dataObject in FormData:
let data = new FormData();
data.append("file", this.state.files);
data.append("data", formData);
formData
is just a Json Object eg: formData = { "a":123 }
Passing data to the node server using put
request with axios
,I am able to get the file object and data in req, but in data field it's giving '[object Object]'
Don't know how to access this. I tried JSON.parse(req.body.data), but getting error
Share Improve this question edited Oct 27, 2019 at 18:35 Prabhat Mishra asked Feb 19, 2019 at 15:45 Prabhat MishraPrabhat Mishra 1,0212 gold badges15 silver badges35 bronze badges 3SyntaxError: Unexpected token o in JSON at position 1
- 2 try using JSON.stringify(data) before sending it to the server – Silvio Biasiol Commented Feb 19, 2019 at 15:46
-
there's not enough context here, but it sounds like you've passed an actual JS object, which natively stringifies to that unhelpful
[object Object]
. You probably want to callJSON.stringify
on it before you send it – Robin Zigmond Commented Feb 19, 2019 at 15:47 -
what is
formData
object? You probably need to doJSON.stringify(formData)
– Andrey Commented Feb 19, 2019 at 15:47
2 Answers
Reset to default 12Looks like formData
is an object that's cast to string [object Object]
prior to being sent on the wire. Try data.append("data", JSON.stringify(formData))
.
Send the object with JSON.stringify and then use JSON.parse once you receive data, to make the string object an object again.