[英]Firebase Auth returning undefined for createUserWithEmailAndPassword
I have a function that creates a new user through Firebase Auth, and for awhile I had it working but now it's returning undefined.我有一个通过 Firebase Auth 创建新用户的函数,有一段时间我让它工作,但现在它返回未定义。 I tried using async await to wait for an input with no help, and I'm not really sure what could be going wrong.
我尝试在没有帮助的情况下使用 async await 等待输入,但我不确定会出现什么问题。 I can't include my Firebase auth code on here, but I put the rest of the code on CodePen:
我不能在此处包含我的 Firebase 身份验证代码,但我将其余代码放在 CodePen 上:
https://codepen.io/TheNomadicAspie/pen/ZEKYwWJ https://codepen.io/TheNomadicAspie/pen/ZEKYwWJ
And here are the relevant function:这是相关的功能:
function newUserSubmitEmail() {
traceFunction()
new_user_dict['email'] = input_text.value.trim()
console.log('newUserSubmitEmail email is ', new_user_dict['email'])
question.innerText = "What password would you like to use?"
input_text.value = ''
right_button.onclick = newUserSubmitPassword
}
function newUserSubmitPassword() {
traceFunction()
new_user_dict['password'] = input_text.value.trim()
console.log('newUserSubmitPassword password is ', new_user_dict['password'])
question.innerText = "Ok, and enter it one more time just to make sure there's no typos."
input_text.value = ''
right_button.onclick = newUserVerifyPassword
}
function newUserVerifyPassword() {
traceFunction()
if (new_user_dict['password'] === input_text.value) {
newUserSubmitEmailAndPassword()
} else {
question.innerText = "Those passwords didn't match. Let's try again."
console.log('newUserVerifyPassword old password was ', new_user_dict['password'], ' new password is ', input_text.value)
input_text.value = ''
right_button.onclick = newUserSubmitPassword
}
}
async function newUserSubmitEmailAndPassword() {
traceFunction()
console.log('newUserSubmitEmailAndPassword email is ', new_user_dict['email'], ' password is ', new_user_dict['password'])
input_text.value = ''
firebase.auth().createUserWithEmailAndPassword(await new_user_dict['email'], await new_user_dict['password']).then((userCredential) => {
console.log('newUserSubmitEmailAndPassword User created successfully')
userCreatedSuccessfully()
})
.catch(async (error) => {
var errorCode = await error.code
var errorMessage = error.message
question.innerText = errorCode
console.log('newUserSubmitEmailAndPassword Error creating user. Error code: ', errorCode, ' ', errorMessage)
if (errorCode === 'auth/email-already-in-use') {
newUserEmailInUse()
} else if (errorCode === 'auth/invalid-email') {
newUserTryEmailAgain()
} else if (errorCode === 'auth/operation-not-allowed') {
errorPleaseRefresh()
} else if (errorCode === 'auth/weak-password') {
newUserTryPasswordAgain()
}
})
}
Any ideas?有任何想法吗?
Maybe it's because you use the await
in the wrong place.也许是因为您在错误的地方使用了
await
。 The code is written with async/await
like this:代码是用
async/await
编写的,如下所示:
try {
const userCredentials = await firebase
.auth()
.createUserWithEmailAndPassword(
new_user_dict["email"],
new_user_dict["password"]
);
console.log(
"newUserSubmitEmailAndPassword User created successfully",
userCredentials.user
);
// comment out this
//userCreatedSuccessfully();
} catch (error) {
var errorCode = await error.code;
var errorMessage = error.message;
question.innerText = errorCode;
console.log(
"newUserSubmitEmailAndPassword Error creating user. Error code: ",
errorCode,
" ",
errorMessage
);
if (errorCode === "auth/email-already-in-use") {
newUserEmailInUse();
} else if (errorCode === "auth/invalid-email") {
newUserTryEmailAgain();
} else if (errorCode === "auth/operation-not-allowed") {
errorPleaseRefresh();
} else if (errorCode === "auth/weak-password") {
newUserTryPasswordAgain();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.