当我设置一些REST服务的匿名回调函数,我得到一个非常奇怪的行为,其中,如果我的REST服务的根本console.log
的结果,我得到预期的有效载荷(一个对象数组)。然而,当我运行在同一个阵列上的循环,并尝试获得一些关键的价值,我得到一个错误,因为显然产品undefined
不确定项打字稿for循环
...
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... :/
}
}
任何想法?必须有某种异步行为发生,但我无法弄清楚。
谢谢!
这不是一个错误打字稿,这仅仅是JavaScript的 –
任何想法,为什么我得到这个错误?由于 –
[一切似乎工作(https://www.typescriptlang.org/play/index.html#src=var%20result%20%3D%20%5B%7Btext%3A%20'foo'%7D% 2C%20%7Btext%3A%20'bar '%7D%5D%3B%0D%0Aconsole.log(结果)%3B%20%2F%2F%20outputs%20%5B%7Btext%3A%20'foo' %7D%2C%20%7Btext%3A%20'bar'%7D%5D%0D%0Afor(让%20item%20of%20result)%7B%0D 0A%%20%20%20%20console.log(项目的.text)%3B%20%2F%2F%20error%20can't%20read%20text%20of%20undefined%0D 0A%%20%20%20%20console.log(项目)%3B%20%2F%2F %20HOWEVER ...%20this%20works ...%20%3A%2F%0D 0A%%7D)如预期。你确定'response'确实是在评论中吗? – martin