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