0

I have a function which fetches data from the database and sets them in an array. But the issue here is that the array created here returns an array object with length 0

my Code

let products = [];
item.order_items.map((x) => {
  Axios.get(api + `/order-items/${x.id}`, {
    headers: {
      Authorization: "Bearer " + user.token,
    },
  }).then((res) => products.push(res.data.product));
});
console.log(products);

This returns enter image description here

which makes the array non-iterable... Is there any fix for this

5
  • It's not quite clear. Even if it's an empty object, how would that make the array non-iterable? Commented Jun 16, 2020 at 15:55
  • 1
    The console.log shows length:3. Is it because of async? Are you trying to manipulate the array before it's been populated? Commented Jun 16, 2020 at 15:56
  • It seems to be async events not yet resolved. Isn't this solves your problem? stackoverflow.com/questions/46930001/… Commented Jun 16, 2020 at 16:07
  • The problem here is that the array is populated with the value but can not run array.map on it as the proto: array(0) Commented Jun 16, 2020 at 16:38
  • I am manipulating the array after it's been populated by the async function. The line where it runs console.log -> array.length returns 0 Commented Jun 16, 2020 at 16:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.