【问题标题】:Callable cloud functions - handle error in android可调用云函数 - 处理 android 中的错误
【发布时间】:2023-02-04 13:49:45
【问题描述】:

我正在尝试从 firestore 和 auth 中删除用户。

我有这个可调用的云函数:

export const deleteUser = functions.https.onCall(async (data, context) => {
    const userEmail = data.userEmail;
    const collection = data.collection;
    
    try {
        deleteUserByEmail(userEmail, collection)
        return "deleted!"
    } catch (error) {
        throw new functions.https.HttpsError('invalid-argument', 'there is no user with that email', error);
    }
})

async function deleteUserByEmail(userEmail: string, collection: string) {
    const auth = admin.auth();
    const db = admin.firestore();
    
    const { uid } = await auth.getUserByEmail(userEmail);
    
    await db.collection(collection)
        .doc(uid)
        .delete();
    await auth.deleteUser(uid);
    
    return uid;
}

在android中我有这个:

fun deleteFromFirebase(){
        val data = hashMapOf(
            "userEmail" to user.email,
            "collection" to "User"
        )

        functions // Optional region: .getInstance("europe-west1")
            .getHttpsCallable("deleteUser")
            .call(data)
            .addOnCompleteListener() { task ->
                if(!task.isSuccessful)
                {
                    Log.d("User", "ERROR")
                    val e = task.exception
                    if (e != null) {
                        Log.d("Admin", e.message.toString())
                    }
                }else{
                    Log.d("User", "Deleted")
                    //make something
                }
            }
    }

如果 auth 中的用户和 firestore 中的文档存在,则效果很好。 但我试图产生一些错误。

所以我从 auth 中删除了用户并运行了该函数。 Android 日志显示D/User: User deleted 但在谷歌云的控制台中:

函数执行耗时 1878 毫秒,完成状态码:200 已完成函数的异常:错误:没有与提供的标识符对应的用户记录。

我如何处理错误并在android中正确获取?谢谢!

【问题讨论】:

    标签: node.js firebase firebase-authentication google-cloud-functions


    【解决方案1】:

    deleteUserByEmail 函数是async 并返回一个Promise。您的 return 语句在承诺得到解决之前运行。尝试重构代码,如下所示:

    export const deleteUser = functions.https.onCall(async (data, context) => {
      const userEmail = data.userEmail;
      const collection = data.collection;
    
      try {
        // add await, continues after Promise is resolved
        await deleteUserByEmail(userEmail, collection)
        return "deleted!"
      } catch (error) {
        console.log(error) // <-- check for any errors
        throw new functions.https.HttpsError('invalid-argument', 'there is no user with that email', error);
      }
    })
    
    async function deleteUserByEmail(userEmail: string, collection: string) {
      const auth = admin.auth();
      const db = admin.firestore();
    
      const { uid } = await auth.getUserByEmail(userEmail);
    
      return await Promise.all([
        db.collection(collection).doc(uid).delete(),
        auth.deleteUser(uid)
      ])
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 2022-08-14
      • 2021-08-29
      • 1970-01-01
      • 2018-05-17
      • 2020-10-03
      • 1970-01-01
      • 2021-08-07
      • 2022-01-15
      相关资源
      最近更新 更多