簡體   English   中英

如何獲得函數的返回錯誤值? (反應本機)

[英]How to get return error value of function? (React Native)

我在新函數中創建async / await(try ... catch)。

我在另一個組件中調用此函數。 然后,我只想返回錯誤值並顯示錯誤值。

我該怎么做?

我的職能:

const updateInformation = async () => {

try {
  const response = await updateAPI({
    variables: {
      data: {
        name: state.name,
        phoneNumber: state.phoneNumber,
      },
    },
  })
} catch (ex) {
  return Utils.extractErrorMessage(ex)
}
}

零件:

const onPressSendButton = () => {
if (name && address1 && address2 && phoneNum) {
  const r = updateInformation()
  // I want to show return error value in this line.
} else {
  return false
}
}

我進行了一些更改,這應該可以工作(並附有說明):

功能

const updateInformation = async () => {
    return updateAPI({ // Missing return
        variables: {
            data: {
                name: state.name,
                phoneNumber: state.phoneNumber,
            },
        },
    });
};

零件

const onPressSendButton = async () => {
    if (name && address1 && address2 && phoneNum) {
        try {
            const r = await updateInformation(); // Need to await here
        } catch (e) {
            // This is where you handle the error
        }
    } else {
      return false;
    }
};

暫無
暫無

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

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