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

Jamilur Rahman
  • 69
  • 2
  • 12
  • It's not quite clear. Even if it's an empty object, how would that make the array non-iterable? – ABGR Jun 16 '20 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? – James Jun 16 '20 at 15:56
  • It seems to be async events not yet resolved. Isn't this solves your problem? https://stackoverflow.com/questions/46930001/javascript-array-not-iterable – theDavidBarton Jun 16 '20 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) – Jamilur Rahman Jun 16 '20 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 – Jamilur Rahman Jun 16 '20 at 16:40

0 Answers0