-1

So , i have three forms to be submitted at one click.

When submitted, the data collected from the three forms must create three rows in the DB through an API triggering a POST request

How do i pass the variable alldata to the API?

If i pass only one of them as data1, it perfectly works. But passing them as an array is not.

Appreciate your help.

const data1 = new FormData()
data1.set("images", values.image1)
data1.set("detail", values.detail1)
const data2 = new FormData()
data1.set("images", values.image2)
data1.set("detail", values.detail2)
const data3 = new FormData()
data1.set("images", values.image3)
data1.set("detail", values.detail3)


var alldata = [data1,data2,data3]



const res =  axios(
"post",
"/APIGOESHERE"
alldata
)

1 Answers1

1

You can only pass one form at a time. You can just put all the parameters in the same FormData object. Give them all the same name and Express will collect them in an array. Use the append() method to add multiple items with the same name; set will replace the item.

const data1 = new FormData()
data1.append("images", values.image1)
data1.append("detail", values.detail1)
data1.append("images", values.image2)
data1.append("detail", values.detail2)
data1.append("images", values.image3)
data1.append("detail", values.detail3)

const res =  axios(
    "post",
    "/APIGOESHERE"
    data1
)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • and how you then access this on the backend? I'm doing something like: upload.fields([ { name: "images", maxCount: 1 }, { name: "detail", maxCount: 1 }, ]), and I get unexpected end of form – Dzsonah Apr 20 '23 at 17:22
  • 1
    Are you using `multer`? It will aid with processing multipart/form-data. You can find tutorials on it. – Barmar Apr 20 '23 at 17:30
  • would you mind having a look at this please? You might spot something that I couldn't: https://stackoverflow.com/questions/76064309/unexpected-end-of-form-at-callfinal-error – Dzsonah Apr 20 '23 at 17:32
  • I'm not a node.js/Express expert. – Barmar Apr 20 '23 at 17:33
  • Oh thats fair enough! Ignore me then, I'll try looking intro this further – Dzsonah Apr 20 '23 at 17:34