我正在尝试在创建用户属性之后设置它的.displayName属性。
问题是:用户需要一些时间来创建,所以在调用.updateProfile()之后就无法使用.createUserWithEmailAndPassword()。那么,我如何在创建之后立即更新.displayName呢?
现在我正试着这样做:
export function signup(currentUser, email, password) {
createUserWithEmailAndPassword(auth, email, password)
updateProfile(currentUser, {displayName : "some name"})
}当我调用这个函数时,我使用这个函数作为currentUser的值
export function useAuth() {
const [ currentUser, setCurrentUser ] = useState();
useEffect(() => {
const unsub = onAuthStateChanged(auth, user => setCurrentUser(user));
return unsub; // Cleaning Function
}, [])
return currentUser;
}发布于 2022-06-06 21:32:02
createUserWithEmailAndPassword()和updateProfile()方法都是异步,并返回承诺。因此,您要么需要把诺言串在一起,要么使用斯科特在评论中提到的async/await。
1/连锁承诺
export function signup(currentUser, email, password) {
createUserWithEmailAndPassword(auth, email, password)
.then(userCredential => {
updateProfile(userCredential.user, {displayName : "some name"})
})
}使用async/await 2/使用
export async function signup(currentUser, email, password) {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
await updateProfile(userCredential.user, {displayName : "some name"})
}发布于 2022-06-06 21:35:03
在updateProfile(currentUser, {displayName : currentUser.displayName})钩子更新了currentUser之后,需要调用useAuth
useEffect(() =>{
if ('userName' in currentUser && currentUser.userName !== '') {
updateProfile(currentUser, {displayName : currentUser.userName})
}
}, [currentUser])
https://stackoverflow.com/questions/72523605
复制相似问题