【发布时间】:2021-01-06 06:02:33
【问题描述】:
我正在使用 createUserWithEmailAndPassword 创建用户,并将用户保存到实时 firebase 数据库。创建用户后,我想在数据库中更新此用户数据。
通常的流程是 createUserWithEmailAndPassword 创建一个用户并将其保存到数据库中,然后我更新该用户的数据。
但是,有时通过 createUserWithEmailAndPassword 创建用户但未将其保存在数据库中,首先设置更新,然后 createUserWithEmailAndPassword 将用户保存到数据库,因此它会覆盖更新。
顺便说一句,我使用 async await 来等待 createUserWithEmailAndPassword 返回的 promise 解决,然后开始更新用户。
const res = await secondaryApp.auth().createUserWithEmailAndPassword(user.email, password)
await secondaryApp.auth().signOut();
if (newAvatar) {
const type = newAvatar.type.slice(6);
const { blob } = newAvatar;
const ref = storage.child(`users/${res.user.uid}.${type}`);
const image = await ref.put(blob);
const imageUrl = await image.ref.getDownloadURL();
user.avatar = imageUrl;
user.thumbnail = imageUrl;
}
await firebase
.database()
.ref(`users/${res.user.uid}`).set(
{
public: {
...user
}
}
).then(
firebase.auth().sendPasswordResetEmail(user.email).then(
done(
"success",
"Successfull user creation.",
`A new user has been created.`
)
)
)
},
【问题讨论】:
标签: javascript firebase firebase-authentication