简体   繁体   English

未处理的拒绝(TypeError):传播不可迭代实例的尝试无效我该如何解决

[英]Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance how can I fix

I am having error like this in my project:我在我的项目中遇到这样的错误:

Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance.未处理的拒绝(TypeError):传播不可迭代实例的尝试无效。 In order to be iterable, non-array objects must have a Symbol.iterator method为了可迭代,非数组对象必须有一个 Symbol.iterator 方法

Here is my SortContracts.ts code:这是我的 SortContracts.ts 代码:

function sortContracts(
  contracts: ContractUsage[],
  sortField?: SortType,
  sortDirection?: SortDirection
): any[] {
  let sortFunction:any
  switch (sortField) {
    // Sort by End Date
    case SortType.DATE_SORT:
      if (sortDirection === SortDirection.ASC) {
        // Sort by End Date ASC
        sortFunction = (a:any, b:any) => endDateSortAscFunc(a, b);
      } else {
        // Sort by End Date DESC
        sortFunction = (a:any, b:any) => endDateSortDescFunc(a, b);
      }
      break;

    // Sort by ContractID
    case SortType.CONTRACT_SORT:
      if (sortDirection === SortDirection.ASC) {
        // Sort by ContractID ASC
        sortFunction = (a:any, b:any) => contractIdSortAscFunc(a, b);
      } else {
        // Sort by ContractID DESC
        sortFunction = (a:any, b:any) => contractIdSortDescFunc(a, b);
      }
      break;

    case SortType.USAGE_ORDER:
      if (sortDirection === SortDirection.ASC) {
        sortFunction = (a:any, b:any) => usageOrderSortAscFunc(a, b);
      } else {
        sortFunction = (a:any, b:any) => usageOrderSortDescFunc(a, b);
      }
      break;
    // default sort
    default:
      sortFunction = (a:any, b:any) => contractIdSortAscFunc(a, b);
  }

  // Return the sorted contracts
  console.log('contract here: ',  [...contracts].sort(sortFunction))
  return [...contracts].sort(sortFunction);
}

在此处输入图片说明

The variable you are trying to spread (ie. [...contracts] ) is not iterable .您试图传播的变量(即[...contracts] )不是iterable If you want use this operator, you need to have this variable iterable .如果你想使用这个运算符, 你需要有这个变量 iterable Check the variable if it is really an array (or any other iterable type) and if it's not null or undefined .检查变量是否真的是一个数组(或任何其他可迭代类型),并且它不是 null 或 undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的拒绝(TypeError):散布不可迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance React + Redux:未处理的拒绝(TypeError):散布不可迭代实例的无效尝试 - React + Redux: Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance 未处理的拒绝(TypeError):破坏非迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to destructure non-iterable instance 我的应用程序运行良好,但现在显示错误 [Unhandled promise 拒绝:TypeError:传播不可迭代实例的无效尝试 - my app was working good but now shows the error [Unhandled promise rejection: TypeError: Invalid attempt to spread non-iterable instance 未处理的运行时错误类型错误:传播不可迭代实例的无效尝试 - Unhandled Runtime Error TypeError: Invalid attempt to spread non-iterable instance React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance 类型错误:传播不可迭代实例和合成事件的尝试无效 - TypeError: Invalid attempt to spread non-iterable instance and Synthetic Events TypeError:传播不可迭代实例的无效尝试 - TypeError: Invalid attempt to spread non-iterable instance 传播不可迭代实例的尝试无效 - Invalid attempt to spread non-iterable instance 将项目添加到 Reducer 中的空数组中会给出错误“类型错误:传播不可迭代实例的尝试无效”。 - Add Items into Empty array in Reducer give Error 'TypeError: Invalid attempt to spread non-iterable instance.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM