簡體   English   中英

解構 JavaScript 返回錯誤

[英]Destructuring JavaScript returns an error

我有一個 function 我正在嘗試從選項中解構幾個參數

  fetchArtifactsWithFiltersOG = async options => {
   
    const { nextPage, fromSearch } = options;

  }

問題:

但是,在某些用例中options are not passed ,在這種情況下,它會拋出nextPage of undefined error

在這種情況下我如何解構,我不確定選項是否只是有時通過。 沒有使我的語法 ES5。

為選項參數設置默認值。

async (options = {}) =>

或者

async (options = { nextPage: 1, frontSearch: false }) =>

您可以添加默認值:

const { nextPage, fromSearch } = options || { nextPage:null, fromSearch:null };

您應該為選項分配默認值:

fetchArtifactsWithFiltersOG = async (options: {}) => {

    const { nextPage, fromSearch } = options;

}

或者

fetchArtifactsWithFiltersOG = async options => {

    const { nextPage, fromSearch } = options || {};

}

暫無
暫無

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

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