Whenever I set an anonymous callback function on a REST service, I encounter a peculiar issue. If I simply use console.log
to display the result of the service call, I am able to see the expected payload (an array of objects). However, when I attempt to iterate over this array using a loop and access the value of a key, I receive an error indicating that the item is undefined
.
...
callback: (result) => {
console.log(result); // outputs [{text: 'foo'}, {text: 'bar'}]
for(let item of result){
console.log(item.text); // error can't read text of undefined
console.log(item); // HOWEVER... this works... :/
}
}
Do you have any insights on what might be causing this issue? It feels like there might be some asynchronous behavior at play, but I'm unable to pinpoint it.
Appreciate your help!