这是我的JS代码:
import { ref } from "vue"
import { projectAuth } from '../firebase/config'
import { getAuth, createUserWithEmailAndPassword, updateProfile } from 'firebase/auth'
const error = ref(null)
const isPending = ref(false)
const signup = async(email, password, displayName) => {
error.value = null
isPending.value = true
try {
const res = createUserWithEmailAndPassword(projectAuth, email, password)
console.log(projectAuth.currentUser)
if (!res) {
console.log('Could not complete the signup')
throw new Error('Could not complete the signup')
}
console.log(projectAuth.currentUser)
await updateProfile(projectAuth.currentUser, {displayName})
error.value = null
isPending.value = false
return res
} catch(err) {
console.log('ERROR: ' + err.message)
error.value = err.message
isPending.value = false
}
}
const useSignup = () => {
return {error, signup, isPending}
}
export default useSignup
我的ve3应用程序在用户注册时调用此脚本中的signup函数。createUserWithEmailAndPassword函数成功,用户显示在firebase中。但是我还想为我的用户添加一个显示名,所以我试图使用updateProfile函数来做到这一点,但是有一个问题。
问题是projectAuth。currentUser是空的,即使创建用户后,我不知道为什么??
createUserWithEmailAndPassword()
方法返回一个promise。由于您的功能是async
,请尝试添加await
:
const res = await createUserWithEmailAndPassword(projectAuth, email, password)
console.log(projectAuth.currentUser)
或者,您可以直接从res将User
对象传递给updateProfile
:
const { user } = await createUserWithEmailAndPassword(projectAuth, email, password)
await updateProfile(user, { displayName })