I'm uploading multiple files separately using axios.
This is the request(all the following code is inside an async function):
const proms = files.map(file => {
let formData = new FormData();
formData.append("file", file);
return axios.post(`/upload/${userId}`, formData)
});
proms will be an array of promises so I proceed to Promise.all it:
const res = await Promise.all(proms)
If I do console.log(res) it shows a successfull response from server as you can see here:
Array(3) [ {…}, {…}, {…} ]
0: Object { data: {…}, status: 200, statusText: "OK", … }
config: Object { timeout: 0, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", … }
data: Object { fieldname: "file", originalname: "612_photo.jpg", encoding: "7bit", … }
destination: ************************
encoding: "7bit"
fieldname: "file"
filename: "/1648309550808-586868206.jpeg"
mimetype: "image/jpeg"
originalname: "612_photo.jpg"
path: ************************
size: 43267
<prototype>: Object { … }
headers: Object { "content-length": "395", "content-type": "application/json; charset=utf-8" }
request: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }
status: 200
statusText: "OK"
<prototype>: Object { … }
1: Object { data: {…}, status: 200, statusText: "OK", … }
2: Object { data: {…}, status: 200, statusText: "OK", … }
length: 3
<prototype>: Array []
But then when I do console.log(res.data) it is undefined. What is happening here?
your response(res) is An array so you cant access res.data ,try :
res.forEach(item=>{console.log(item.data)})