首页 > 解决方案 > 无法在 react-native 中捕获错误

问题描述

如何处理 React-Native 中的错误

所以这是我的错误枚举

export const frontendErrors = {
    INVALID_PHONE_NUMBER_ENTERED: 'Sorry you have entered invalid phone number'
}

从我的 phoneAuth.js 文件中,我在这样的 catch 语句中抛出错误

catch (err) {
    const errorSearch = 'auth/invalid-phone-number'
    if (err.message.includes(errorSearch)) {
     throw new Error(frontendErrors.INVALID_PHONE_NUMBER_ENTERED)
    }

当我在父组件中发现错误时

catch (error) {
           console.log(error)
            this.dropDownAlertRef.alertWithType('error', 'Error', error)
        }

在日志中,我将错误作为一个对象,就像这样

Error: Sorry you have entered invalid phone number

接受字符串,dropdownAlertRef因此它正在记录一个空对象,即

this.dropDownAlertRef.alertWithType('error', 'Error', 'Invalid phone number')

这将记录错误。

我尝试了什么?

做,console.log(error.Error)给出undefined。如何访问我的错误?

标签: reactjsreact-native

解决方案


你可以做这样的事情。

console.log(error.message)

如需更多参考,您可以参考这里


推荐阅读