Javascript 类型错误:传播不可迭代实例的尝试无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53991403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Invalid attempt to spread non-iterable instance
提问by Freddy
After compiling to android and downloading via Store I get the error:
编译到android并通过Store下载后,出现错误:
"TypeError: Invalid attempt to spread non-iterable instance"
But using "react-native run-android" creates no error message therefor I can't find a good way to debug it.
但是使用“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
});
});
Thanks for any help.
谢谢你的帮助。
采纳答案by Freddy
I removed prevData and replaced it with this.state.articlesData. I also change the logic so at the begining when articlesData is empty, it doesn't merge two objects, instead just uses the response.data.
我删除了 prevData 并将其替换为 this.state.articlesData。我还更改了逻辑,因此在开始时articlesData 为空时,它不会合并两个对象,而是仅使用 response.data。
if(propSearch=="" || propSearch==null && this.state.currentPage!=1 && refresh!=true){
newData=[...this.state.articlesData,...response.data]
}
else{
newData=response.data
}
this.state.currentPage!=1 is pretty much the same as oldData != empty
this.state.currentPage!=1 与 oldData != empty 几乎相同
It workes now.
它现在起作用了。
回答by maulikdhameliya
I was getting this crash in release android build only. release ios build and android debug build working perfectly.
我仅在发布 android 版本中遇到此崩溃。发布 ios 构建和 android 调试构建完美运行。
After spending a few hours found solutions from the internet.
花了几个小时后从互联网上找到了解决方案。
edit your .babelrcand add following into your plugins
编辑您的.babelrc并将以下内容添加到您的插件中
[
"@babel/plugin-transform-spread",
{
"loose": true
}
]
so Here is my .babelrcfile
所以这是我的.babelrc文件
{
"presets": [
"module:metro-react-native-babel-preset"
],
"plugins": [
"syntax-trailing-function-commas",
"@babel/plugin-transform-flow-strip-types",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-regenerator",
"@babel/plugin-transform-async-to-generator",
"@babel/plugin-transform-runtime",
[
"@babel/plugin-transform-spread",
{
"loose": true
}
]
],
"sourceMaps": true
}
I hope this answer helps someone and save few hours :)
我希望这个答案可以帮助某人并节省几个小时:)
回答by Drew Reese
This is because it is a runtime error, not a "compile time" error.
这是因为它是运行时错误,而不是“编译时”错误。
Is there a line number associated with the error? Based on the question being about the spread operator I'll assume it's this line: newArticlesData=[...prevData,...response.data]. I assume your prevDatais iterable, but is your response data? Try newArticlesData=[...prevData, response.data]?
是否有与错误相关的行号?基于这个问题是关于传播经营者,我会以为这是这一行:newArticlesData=[...prevData,...response.data]。我假设您prevData是可迭代的,但是您的响应数据是吗?试试newArticlesData=[...prevData, response.data]?
Here's an example of invalid spread operator use:
以下是无效传播运算符使用的示例:
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');

