Currently, I struggle with some weird js array behavior, which is new to me. Would be great if someone can help me out with that. So this is my code:
getData(strings) {
let unsortedKeys = ['key1', 'key2'];
strings.forEach(data => {
ref.once("value").then(data => {
// doesn't matter what will be pushed to the array
unsortedKeys[2] = 'key3';
});
});
return unsortedKeys;
}
I know this code will not work. I've simplified the Firebase promise for this example. By the way, I'm using Angular with Typescript.
The result:
console.log(getData());
If i know iterate over the array just the values of [0]
and [1]
will be shown. Also accessing the field via index getData()[2]
is not possible. I'm not sure if this happens cause the async behavior of promises, but it seems really strange to me, since the value and the index are there.
Does anyone have an idea why this happens?