首页 > 解决方案 > 如何使用 firebase 9 显示错误消息

问题描述

以下代码有效,如果出现问题,我会收到错误消息。注意,这是 Vue 代码。因此.value

import { getAuth, createUserWithEmailAndPassword } from 'firebase/auth'

const register = async () => {
  loading.value = true
  let response
  try {
    const auth = getAuth()
    response = await createUserWithEmailAndPassword(
      auth,
      form.value.email,
      form.value.password
    )
  } catch (err) {
    console.log(err.message)
  }
  loading.value = false

  return response
}

但是,这是返回的示例error.message

Firebase: Password should be at least 6 characters (auth/weak-password).

我的问题。有没有办法让一个干净的消息回来?我的意思是没有Firebase: and (auth/weak-password)

或者我在这里错过了什么?我应该用另一种方法来处理 Firebase 错误对象吗?也许我应该error.code为每个场景自己编写自定义消息?

让我知道是否需要任何其他信息,我会更新问题:)

标签: firebasefirebase-authentication

解决方案


import { getAuth, createUserWithEmailAndPassword } from 'firebase/auth'

const register = async () => {
  loading.value = true
  let response
  try {
   if(form.value.password.length <= 6 ){
     return "Password should be at least 6 characters (auth/weak-password)"
   }

    const auth = getAuth()
    response = await createUserWithEmailAndPassword(
      auth,
      form.value.email,
      form.value.password
    )
  } catch (err) {
    console.log(err.message)
  }
  loading.value = false

  return response
}

推荐阅读