簡體   English   中英

未處理的拒絕(TypeError):傳播不可迭代實例的嘗試無效我該如何解決

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

我在我的項目中遇到這樣的錯誤:

未處理的拒絕(TypeError):傳播不可迭代實例的嘗試無效。 為了可迭代,非數組對象必須有一個 Symbol.iterator 方法

這是我的 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);
}

在此處輸入圖片說明

您試圖傳播的變量(即[...contracts] )不是iterable 如果你想使用這個運算符, 你需要有這個變量 iterable 檢查變量是否真的是一個數組(或任何其他可迭代類型),並且它不是 null 或 undefined

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM