7

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

3
  • 2
    try using JSON.stringify(data) before sending it to the server 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 call JSON.stringify on it before you send it Commented Feb 19, 2019 at 15:47
  • what is formData object? You probably need to do JSON.stringify(formData) Commented Feb 19, 2019 at 15:47

2 Answers 2

12

Looks 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)).

Sign up to request clarification or add additional context in comments.

1 Comment

Don't you mean JSON.stringify(formData) ? Or did I miss something.
0

Send the object with JSON.stringify and then use JSON.parse once you receive data, to make the string object an object again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.