【问题标题】:firebase firestore wrap updateProfile function inside a transactionfirebase firestore 在事务中包装 updateProfile 函数
【发布时间】:2019-04-09 12:26:31
【问题描述】:

我将一些更新包装在一个事务中,以使它们具有原子性。例如,当用户更新他的 displayName(存储在 users 集合中)时,firebase auth 的 displayName 也将被更新。我想在事务中进行这两项更新。到目前为止,我看到的所有事务示例都是关于更新某个集合中的某些文档数据。如何将身份验证函数包装在事务中或将它们与现有事务相关联?有可能吗?

我有这个 sn-p:

        db.runTransaction((transaction)=>{
            return transaction.get(displayNameCheckRef).then((snapshot)=>{
                var newDisplayName = this.state.name;
                if(snapshot.empty){
                    transaction.set(currentUserRef, {displayName:newDisplayName});
                }
            });
        })
        .then((newDisplayName)=>{
            console.log("display name changed to " + newDisplayName);
        })
        .catch((error)=>{
            console.log("transaction error.");
            console.log(error);
        });

现在我想将下面的 updateProfile 函数与上面的事务关联起来。我该怎么做?

   user.updateProfile({
       displayName: this.state.name
   }).then(() => {
     //do some stuff here
   });

非常感谢!

【问题讨论】:

标签: javascript firebase react-native transactions google-cloud-firestore


【解决方案1】:

您尝试做的事情是不可能的。 Firestore 事务只能处理与项目关联的单个数据库中的文档。它们不会影响其他产品,例如 Firebase 身份验证或 Cloud Storage for Firebase。

如果您需要对多个产品进行原子操作,您的代码将需要手动回滚来自多个产品的更新,而这些回滚仍然可能出现错误,因此您需要在出现故障时提出一些回退行为.

【讨论】:

    猜你喜欢
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    相关资源
    最近更新 更多