【发布时间】: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