I am creating a web-app and have something like this:
this.arr = [];
(async function f() {
try {
const response = await this.myService.getData();
const data = response.data;
data.forEach((entry) => {
this.arr.push(new MyObject(entry.valueOne, entry.valueTwo));
});
} catch (error) {
console.log(error);
}
}).call(this);
Now when I query this.arr
I get the following output:
[]
0: Object { valueOne: "foo", valueTwo: "bar" }
1: Object { valueOne: "foobar", valueTwo: "foo" }
length: 2
<prototype>: Array []
So try to log only the first element via
this.console.log(this.arr[0]);
and I get
TypeError: this.arr[0] is undefined
I think the problem is, that I push elements to an empty array, because when I create the array by hand like
console.log(
[
{name: "foo", description: "bar"},
{name: "foobar", description: "foo"},
]
);
I get the following output:
(2) […]
0: Object { name: "foo", description: "bar"}
1: Object { name: "foobar", description: "foo"}
length: 2
<prototype>: Array []
Almost the same, except that the length of the hand-generated array is displayed in the brackets.