[英]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.