0

i have the following function:

const sendMyPhoto = ({ token, data }) => {
  console.log(typeof data) // Its returns: object
  console.log(data.length) // Its returns: 0
  console.log(data)
  /**
  its returns the correct data:
  [  0: {annoucements_id: "105", image: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD… 
  m2XmfQfRt68zSYlJyzaO5+yc/E2If3OITlHXo/qGvT1jon//Z"}
  1: {annoucements_id: "105", image: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD… 
 +cr5yvnK+cr5yvnK+cr5yuV85Xzlcr5yvnK+cr/FfOXP/2Q=="}
  length: 2
  __proto__: Array(0) ]
  **/
  return Http.post(`/api/vehicle/image/`, data, { headers: { Authorization: 'Bearer ' + token } })
}

This is my network tab: enter image description here

I don't understand what's going on. Why data.length returns 0, and the axios send a empty array, when the console.log(data) returns the correct data.

  • 3
    `console.log(data)` shows the correct data [because it doesn't actually evaluate the value when the log is called](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays). Try to log `console.log(JSON.stringify(data))`. This means that `data` is populated after you start the AJAX call. We can't answer why that is, because you haven't shown how `data` is populated. – Ivar Dec 04 '19 at 19:40
  • 1
    Thanks a lot ivar! Now I understand what is happening with my code and I can solve my problem – Felipe Rodrigues Dec 04 '19 at 19:52

0 Answers0