【发布时间】:2019-08-11 09:29:13
【问题描述】:
我有这个使用电子邮件和密码创建 firebase 用户的 firebase 方法
async register(name, email, password,type) {
let id;
const createUser = this.functions.httpsCallable('createUser');
return await this.auth.createUserWithEmailAndPassword({email,password })
.then((newUser)=>{
id = newUser.user.uid;
newUser.user.updateProfile({
displayName: name
})
})
.then(()=>{
createUser({
id:id,
name:name,
email:email,
type:type
})
})
}
它还使用获取用户详细信息和用户类型的云函数将用户添加到 Firestore 集合。
我有 3 个承诺(
createUserWithEmail...()updateUserProfile()1createUser()
) 它们相互依赖。如何在一个功能中使用它们?
NB:由于用户类型字段,无法使用functions.auth.user().onCreate() 方法
如何在不使用 .then() 的情况下编写此方法?
一些用户没有出现在数据库中
【问题讨论】:
-
如果我没听错的话,有些用户不在数据库中,所以它没有输入
.then子句,这就是问题所在。如果我的描述是正确的,那么解决方案是使用.catch子句,它将进入一个被拒绝的承诺。如果代码不会进入then子句,则意味着承诺被拒绝,请使用.catch(reason)处理它。此外,在您的代码中,您将aync-await语法与本机承诺混合在一起。await暂停函数的执行,直到 promise 被解决或拒绝。如果你使用await,你甚至不需要.then。 -
在
updateProfile之后(总是)调用createUser似乎很奇怪——这两个函数都是异步的并且它们都返回一个Promise 吗?
标签: javascript firebase google-cloud-firestore firebase-authentication google-cloud-functions