HarmonyOS (react-native)promise.reject抛出异常对象只能接收到message参数

turboModule.ts使用promise.reject抛出异常对象。

checkDeviceSupportLocalAuthenticationWithBiometricsOnly(bool: boolean): Promise<{
  code?: number | undefined;
  errorCode?: string | undefined;
  note?: string | undefined
}> {
  try {
    userAuth.getAvailableStatus(userAuth.UserAuthType.FINGERPRINT, userAuth.AuthTrustLevel.ATL2);
    return Promise.resolve({})
  } catch (error) {
    try {
      userAuth.getAvailableStatus(userAuth.UserAuthType.FACE, userAuth.AuthTrustLevel.ATL2);
      return Promise.resolve({})
    } catch (error) {
      // 如果仅支持生物方式,则不考虑密码
      if (bool) {
        return Promise.reject({code: 0, message: '该设备暂不支持生物认证'})
      }else {
        try {
          userAuth.getAvailableStatus(userAuth.UserAuthType.PIN, userAuth.AuthTrustLevel.ATL2);
          return Promise.resolve({})
        }catch (error) {
          return Promise.reject({code: 0, message: '该设备暂不支持生物认证'})
        }
      }
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

在rn catch接收参数时只能拿到message参数:

static checkDeviceSupportLocalAuthenticationWithBiometricsOnly = (biometricsOnly: boolean, callback: (obj: {
  code?: number,
  errorCode?: string,
  note?: string
}) => void) => {
  RNLocalAuthentication?.checkDeviceSupportLocalAuthenticationWithBiometricsOnly(biometricsOnly)
    .then(() => {
      callback({ code: 1 })
    }).catch(({ code, message }) => {
    console.log(`====>>>>指纹认证失败,code:${code},message:${message}`)
    callback({ code, note: message })
  });
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
HarmonyOS
2024-12-25 09:55:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

这个问题是RN内部的规范,当前RN版本规定只能返回字符串,或者返回的对象中包含“message: string”字段。可以通过以下方法进行规避:在原生侧将结果对象转换为JSON字符串返回,JS侧再通过解析JSON,将对象解析出来。

分享
微博
QQ
微信
回复
2024-12-25 12:37:22
相关问题
HarmonyOS react-native应用编译失败
853浏览 • 1回复 待解决
HarmonyOS react-native如何自行打包
1160浏览 • 1回复 待解决
HarmonyOS react-native集成问题
859浏览 • 1回复 待解决
HarmonyOS 使用react-native中的Image问题
731浏览 • 1回复 待解决
鸿蒙生态中有没有react-native适配?
14400浏览 • 1回复 待解决
HarmonyOS RN reject异常问题
645浏览 • 1回复 待解决
react-native 第三方库使用npm更新
2158浏览 • 1回复 待解决
HarmonyOS 捕获异常抛出为什么报错
904浏览 • 1回复 待解决
调用 window.getWindowAvoidArea 时抛出异常
2561浏览 • 1回复 待解决
HarmonyOS 适配 React native
1198浏览 • 1回复 待解决