Why is obj[i]
undefined inside .then()
?
obj = [{'id': 1, 'name': 'john', 'age': '22', 'group': 'grA'}, {'id': 2, 'name': 'mike', 'age': '24', 'group': 'grA'}, {'id': 3, 'name': 'anne', 'age': '25', 'group': 'grB'}]
for (var i = 0; i < obj.length; i++) {
console.log(obj[i]) // has the right value
this.commentService.getAllComments(obj[i].id).then((res) => {
...
console.log(obj[i]) // undefined ???
})
}
Is there any posibility I can solve this situation and is it there an explanation why is undefined? Thank you for your time!
EDIT: The problem was that I was using var
instead of let
. Thank you!