文章内容

2020/9/14 10:43:40,作 者: 黄兵

ERROR TypeError: t is not iterable

最近在迭代一个错误消息的时候出现如下错误:

ERROR TypeError: t is not iterable

具体迭代内容如下:

  1. message"Http failure during parsing for https://america.materialtools.com/verify"
  2. name"HttpErrorResponse"
  3. okfalse
  4. status200
  5. statusText"OK"
  6. url"https://america.materialtools.com/verify"

出现错误的原因:

这个值作为 for…of  的表达式右值,或者作为一个函数的参数,如 Promise.all 或者 TypedArray.from, 不是一个 可迭代对象.  一个可迭代对象可以是一个内置可迭代类型,如Array, String 或 Map, 一个 generator 生成结果, 或者一个实现了 可迭代协议 的对象。

具体错误写法:

for (const entry of error) {
console.log(entry);
}

解决方案:

做为替代必须使用 Object.keys 或 Object.entries 来迭代对象的属性或属性值:

var obj = { 'France': 'Paris', 'England': 'London' };
// 迭代属性名称:
for (let country of Object.keys(obj)) {
    var capital = obj[country];
    console.log(country, capital);
}

for (const [country, capital] of Object.entries(obj))
    console.log(country, capital);


参考资料:

1、TypeError: 'x' is not iterable


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - ERROR TypeError: t is not iterable

分享到:

发表评论

评论列表