如果Firebase触发器中发生错误,如何向客户端抛出错误?

如果Firebase触发器中发生错误,如何向客户端抛出错误?,firebase,triggers,firebase-authentication,google-cloud-firestore,google-cloud-functions,Firebase,Triggers,Firebase Authentication,Google Cloud Firestore,Google Cloud Functions,我的用户集合中有一个更新相应用户配置文件的onUpdate触发器 exports.onUpdateUser = functions.firestore.document('/users/{uid}') .onUpdate((change, context) => { const { uid } = context.params; const { email, displayName, photoURL } = change.after.data(); return

我的用户集合中有一个更新相应用户配置文件的onUpdate触发器

exports.onUpdateUser = functions.firestore.document('/users/{uid}')
.onUpdate((change, context) => {
    const { uid } = context.params;
    const { email, displayName, photoURL } = change.after.data();

    return admin.auth().updateUser(uid, {
        email,
        displayName,
        photoURL
    });
});
如果提供的电子邮件已经存在,触发器可能会引发错误。在这种情况下,我希望放弃对用户文档所做的更改,然后让客户端知道该错误

通过上述实现,文档将被成功更新,触发器将在客户端不知道的情况下无声地抛出错误


我是否可以更改此行为,或者我唯一的选择是实现一个单独的HTTP云函数来处理用户更新?

如果出现错误,您可以使用云函数写入
/users/{uid}
节点下的特定子节点,并让您的客户端前端侦听此子节点

云函数代码如下所示:

exports.onUpdateUser = functions.firestore.document('/users/{uid}')
.onUpdate((change, context) => {
    const { uid } = context.params;
    const { email, displayName, photoURL } = change.after.data();

    return admin.auth().updateUser(uid, {
        email,
        displayName,
        photoURL
    })  
    .catch(error => {
        if (error == "auth/email-already-exists") {

           return admin.database().ref('/users/' + uid).set({updateError: true})
           .catch(error => {
                console.log("Error reporting email error");
                return false;
           });

        } else {
            console.log("Error updating user:", error);
            return false;
        }
    });

});
在前端,收听
/users/{uid}/updateError
节点,如果写入了新值,则显示警报/消息。确切的侦听器语法取决于前端技术(web应用程序?Android应用程序?iOS应用程序?)

请注意,在云函数中,您可以利用
admin.auth().updateUser()
方法的错误管理以及它返回特定代码的事实

其中指出:

如果提供的uid与现有用户不对应,则 提供的电子邮件或电话号码已被现有用户使用, 或者由于任何其他原因,上述方法无法更新用户 由于错误而失败


如果出现错误,您可以使用Cloud函数写入
/users/{uid}
节点下的特定子节点,并让客户端前端监听该子节点

云函数代码如下所示:

exports.onUpdateUser = functions.firestore.document('/users/{uid}')
.onUpdate((change, context) => {
    const { uid } = context.params;
    const { email, displayName, photoURL } = change.after.data();

    return admin.auth().updateUser(uid, {
        email,
        displayName,
        photoURL
    })  
    .catch(error => {
        if (error == "auth/email-already-exists") {

           return admin.database().ref('/users/' + uid).set({updateError: true})
           .catch(error => {
                console.log("Error reporting email error");
                return false;
           });

        } else {
            console.log("Error updating user:", error);
            return false;
        }
    });

});
在前端,收听
/users/{uid}/updateError
节点,如果写入了新值,则显示警报/消息。确切的侦听器语法取决于前端技术(web应用程序?Android应用程序?iOS应用程序?)

请注意,在云函数中,您可以利用
admin.auth().updateUser()
方法的错误管理以及它返回特定代码的事实

其中指出:

如果提供的uid与现有用户不对应,则 提供的电子邮件或电话号码已被现有用户使用, 或者由于任何其他原因,上述方法无法更新用户 由于错误而失败