Javascript TypeError:传播不可编辑实例的尝试无效

Javascript TypeError:传播不可编辑实例的尝试无效,javascript,react-native,react-native-android,Javascript,React Native,React Native Android,编译到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) {

编译到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
    }); 
});

感谢您的帮助。

这是因为这是一个运行时错误,而不是“编译时”错误


是否有与错误相关联的行号?基于关于spread运算符的问题,我假设这是一行:
newArticlesData=[…prevData,…response.data]
。我假设您的
prevData
是可编辑的,但是您的响应数据是吗?尝试
newArticlesData=[…prevData,response.data]

以下是无效扩展运算符使用的示例:

函数trySpread(对象){
let数组;
试一试{
数组=[…对象];
log('无错误',数组);
}捕获(错误){
console.log('error',error);
}
}
//错误
trySpread({});
trySpread({foo:'bar'});
trySpread(4);
//无误
trySpread([]);
trySpread(['foobar']);

trySpread(“foobar”)我删除了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
}
这个.state.currentPage=1与旧数据几乎相同!=空的


它现在可以工作了。

我只是在android版本中遇到了这个崩溃。发布的ios版本和android调试版本运行良好

花了几个小时后,从互联网上找到了解决方案

编辑您的
.babelrc
,并将以下内容添加到插件中

[
      "@babel/plugin-transform-spread",
      {
        "loose": true
      }
    ]
这是我的
.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
}


我希望这个答案能帮助别人并节省几个小时:)

我在iOS 7中的React应用程序中遇到了相同的错误,尽管Babel被配置为传输任何spread运营商。最终,我的
node\u模块
中的导入解决了一些奇怪的问题。删除我的
node\u modules
目录并通过
warn install
(或
npm install
)重新安装后,错误得到解决。

缺少
关闭
.catch
是否有与错误相关联的行号?基于关于spread运算符的问题,我假设这是一行:
newArticlesData=[…prevData,…response.data]
。我假设您的
prevData
是可编辑的,但是您的响应数据是吗?请尝试
newArticlesData=[…prevData,response.data]
?尽管我在前面的检查中看到您设置了
prevData={}
,但它不能扩展到数组中。@Shakespear只是没有粘贴它。添加了它。这就是我正在使用的API:。我将在这两个对象上尝试trySpread()。但是newArticlesData=[…prevData,response.data]不起作用,然后我在代码中得到一个错误,对象是“空的”,这很奇怪。我把“trySpread(prevData)trySpread(response.data)”放在这个.setState之前,根本没有控制台日志。是的,那个例子只是为了说明传播不可复制对象可能会失败。这拯救了我的大脑。我一直在为这个问题苦苦挣扎,不知为什么会这样?