javascript - TypeError:传播不可迭代实例的无效尝试
问题描述
编译到 android 并通过 Store 下载后出现错误:
"TypeError: Invalid attempt to spread non-iterable instance"
但是使用“react-native run-android”不会产生错误消息,因此我找不到调试它的好方法。
fetch(url)
.then(response => response.json())
.then(response => {
if (this._mounted) {
// let dataSource = this.state.articlesDataSource.cloneWithRows(response.data || [])
//var rowCount = dataSource.getRowCount();
var rowCount = Object.keys(response.data).length;
if (refresh == true) {
prevData = {};
} else {
prevData = this.state.articlesData;
}
if (propSearch == "" || propSearch == null) {
newArticlesData = [...prevData, ...response.data];
} else {
newArticlesData = response.data;
}
if (response.meta.next_page != null) {
var rowCount = true;
} else {
var rowCount = Object.keys(newArticlesData).length;
}
if (this._mounted) {
this.setState({
isLoading: false,
//articlesDataSource: this.state.articlesDataSource.cloneWithRows(response.data),
articlesData: newArticlesData,
nextPage: response.meta.next_page,
fetchUrl: url,
rowCount: rowCount
});
}
}
})
.catch(error => {
this.setState({
errorFound: true,
errorMassage: error,
isLoading: false
});
});
谢谢你的帮助。
解决方案
这是因为它是运行时错误,而不是“编译时”错误。
是否有与错误相关的行号?基于关于扩展运算符的问题,我假设它是这一行:newArticlesData=[...prevData,...response.data]
。我假设您prevData
是可迭代的,但是您的响应数据是吗?试试newArticlesData=[...prevData, response.data]
?
这是一个无效传播运算符使用的示例:
function trySpread(object) {
let array;
try {
array = [...object];
console.log('No error', array);
} catch(error) {
console.log('error', error);
}
}
// error
trySpread({});
trySpread({foo: 'bar'});
trySpread(4);
// no error
trySpread([]);
trySpread(['foobar']);
trySpread('foobar');
推荐阅读
- sql-server - 根据条件加入?
- android - 如何区分Android上的指纹和面部ID?
- private-key - 我在哪里可以找到可以生成每个可能的私钥的脚本?
- python - += 运算符在类变量上的行为与我预期的不同
- html - 如何使背景图像居中
- c - 管道打开时管道“错误地址”
- apache-spark-sql - 如何替换 Spark-dataframe 中的字符,如 python pandas-dataframe?
- java - 如何使用 Spring Boot 从 S3 下载 json 文件?
- bixby - 如何在 Bixby 布局中指定图像 url
- firebase - 访问 Firebase 托管网站时出错,提示“您的计算机或网络可能正在发送自动查询”