英文:
firebase.auth() error in createusermethod in java Script
问题
以下是您的JavaScript代码的翻译部分:
function logbtn() {
var email = document.getElementById("inemail").value;
var password = document.getElementById("inpass").value;
var password1 = document.getElementById("inpass1").value;
var lPassword = password.length;
var lPassword1 = password1.length;
if (lPassword < 7) {
alert("密码应至少包含七个字符");
return;
} else if (password != password1) {
alert("请正确输入密码");
return;
} else if (password == password1 || lPassword1 == lPassword) {
alert("测试中");
firebase.auth().createUserWithEmailAndPassword(email.toString(), password.toString()).catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('密码太弱。');
} else {
alert(errorMessage);
}
console.log(error);
});
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
var email = user.email;
alert("创建成功");
} else {
alert("抱歉,请重试");
}
});
}
}
请注意,上述代码中的内容是对您提供的JavaScript代码的翻译。如果您有任何问题或需要进一步的帮助来查找潜在的错误,请随时提问。
英文:
This is my java script for Register page for web using firebase authentication.When i fill the email and password till Alert message Testing will execute but firebase.auth()..... it will not execute and the page will get refresh automatically.
var email=document.getElementById("inemail").value;
var password=document.getElementById("inpass").value;
var password1=document.getElementById("inpass1").value;
var lPassword=password.length;
var lPassword1=password1.length;
if(lPassword < 7)
{
alert("Password Should more than seven Charecter");
return;
}
else if(password!=password1){
alert("Correctly Enter the Password");
return;
}
else if(password==password1 || lPassword1==lPassword)
{
alert("Testing");
firebase.auth().createUserWithEmailAndPassword(email.toString(),password.toString()).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
});
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
var email = user.email;
alert("Sucessfully Created");
}
else {
alert("sorry Try again");
}
});
}
}
in This script have any errors?if it have error please help me to find-out.
答案1
得分: 2
正如Ticherhaz在他的评论中解释的那样,您需要使用then()
方法来检测由异步createUserWithEmailAndPassword()
方法返回的Promise何时被满足。
以下代码应该可以解决问题:
firebase.auth().createUserWithEmailAndPassword(email.toString(), password.toString())
.then(userCredential => {
var email = userCredential.user.email;
alert("Successfully Created");
})
.catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
});
}
请注意,正如文档中所解释的那样,“成功创建用户帐户后,此用户也将登录到您的应用程序”,因此在这种情况下不需要使用onAuthStateChanged()
方法。
在您上面的一条评论中,您说“只有在警告消息'testing'之前,此脚本正在执行”。实际上,如果新用户创建没有错误,那么传递给catch()
的拒绝处理程序回调不会被调用,因此您无法获得createUserWithEmailAndPassword()
正确执行的反馈。为此,您需要使用then()
方法。
英文:
As Ticherhaz explains in his comment, your need to use the then()
method to detect when the Promise returned by the asynchronous createUserWithEmailAndPassword()
method is fulfilled.
The following should do the trick:
firebase.auth().createUserWithEmailAndPassword(email.toString(),password.toString())
.then(userCredential => {
var email = userCredential.user.email;
alert("Sucessfully Created");
})
.catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
});
}
Note that, as explained in the doc, "on successful creation of the user account, this user will also be signed in to your application", so you don't need to use the onAuthStateChanged()
method in this case.
In one of your comments above you say "only till alert message 'testing' this script is executing". Actually, if there is no error with the new user creation, the rejection handler callback passed to catch()
is not called and therefore you don't get a feedback on the fact that the createUserWithEmailAndPassword()
was correctly executed. For that you need the then()
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论