类型错误:传播不可迭代实例的尝试无效 [英] TypeError: Invalid attempt to spread non-iterable instance

查看:49
本文介绍了类型错误:传播不可迭代实例的尝试无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译到android并通过Store下载后出现错误:

After compiling to android and downloading via Store I get the error:

"TypeError: Invalid attempt to spread non-iterable instance"

但是使用react-native run-android"不会产生任何错误消息,因此我找不到调试它的好方法.

But using "react-native run-android" creates no error message therefor I can't find a good way to debug it.

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
    }); 
});

感谢您的帮助.

推荐答案

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

This is because it is a runtime error, not a "compile time" error.

是否有与错误相关的行号?基于关于展开运算符的问题,我假设它是这一行:newArticlesData=[...prevData,...response.data].我假设您的 prevData 是可迭代的,但是您的响应数据是吗?试试 newArticlesData=[...prevData, response.data]?

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 prevData is iterable, but is your response data? Try 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');

这篇关于类型错误:传播不可迭代实例的尝试无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆