前端开发问题:TypeError: records is not iterable
- 在 JavaScript 开发,遇到如下问题
TypeError: records is not iterable
# 翻译TypeError:records 不是可迭代对象
问题原因
-
这个错误表明尝试对一个不可迭代的 records 变量使用迭代操作
-
不可迭代的 records 变量可能是 null、undefined 等
-
迭代操作有例如,
for of
循环、展开运算符...
、Array.from()
问题复现
- 不可迭代变量与
for of
循环
const records = undefined;for (const item of records) {console.log(item);
}
# 输出结果Uncaught TypeError: undefined is not iterable
- 不可迭代变量与展开运算符
...
const records = null;const arr = [...records];console.log(arr);
# 输出结果Uncaught TypeError: undefined is not iterable
- 不可迭代变量与
Array.from()
const records = undefined;const arr = Array.from(records);console.log(arr);
# 输出结果Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))