1

State is like

state = {
courseTitle: "",
courseImage: "",
duration: "",
overView: "",
syllabus: [
  {
    title: "",
    description: "",
  },
 ],
};

And I'm submitting data using FormData() because i have image too

handleSubmit = (e) => {
e.preventDefault();
const fd = new FormData();
fd.append("courseTitle", this.state.courseTitle);
fd.append("courseImage", this.state.courseImage);
fd.append("duration", this.state.duration);
fd.append("overView", this.state.overView);
fd.append("syllabus", this.state.syllabus);

this.props.addCourse(fd);
};

Everythings is fine except syllabus because it is in array, FormData send a value of syllabus,

syllabus:[object object]

Please Can anyone help me on how to send Array Data with the above state format, I'm using react and redux for backend nodejs, express and mongodb

2 Answers 2

3

Easiest way i am aware of is to stringify the array and append. then parse on the backend.

// On frontend
fd.append("syllabus", JSON.stringify(this.state.syllabus));

// On backend
JSON.parse(syllabus)
Sign up to request clarification or add additional context in comments.

Comments

2

One way could be to send (and aptly change the server-code or else check for the valid request spec with api documentation) values passed through JSON.stringify. In your example append the syllabus as fd.append("syllabus", JSON.stringify(this.state.syllabus)).

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.