Javascript 如果函数运行,则不运行其他函数
有人能告诉我为什么这个函数的Javascript 如果函数运行,则不运行其他函数,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,有人能告诉我为什么这个函数的else没有运行,而if运行吗?还有,是不是有什么东西使代码运行缓慢?下面是我的代码 function SignUserIn() { // Get elements const Email = document.getElementById("txtEmail"); const Password = document.getElementById("txtPassword"); // Get email and pass const email = Email.va
else
没有运行,而if
运行吗?还有,是不是有什么东西使代码运行缓慢?下面是我的代码
function SignUserIn() {
// Get elements
const Email = document.getElementById("txtEmail");
const Password = document.getElementById("txtPassword");
// Get email and pass
const email = Email.value;
const password = Password.value;
const auth = firebase.auth();
//Sign In
firebase.auth().signInWithEmailAndPassword(email, password)
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password' || errorCode === 'auth/invalid-email' || errorCode === 'auth/user-disabled' || errorCode === 'auth/user-not-found') {
window.alert(errorMessage);
document.getElementById("txtPassword").value = "";
}
else {
//Realtime listener
firebase.auth().onAuthStateChanged(frebaseUser => {
sessionStorage.setItem("email", email);
window.alert("You Are Successfully Signed In! Welcome " + email);
window.location = "homepage.html";
});
}
});
}
.catch()
方法仅在承诺被拒绝时运行。如果希望在承诺解析或被拒绝时运行代码,则应使用.then()
。它有两个函数参数,一个表示成功,另一个表示拒绝
firebase.auth().signInWithEmailAndPassword(email, password)
.then(function() {
// Handle success here
firebase.auth().onAuthStateChanged(frebaseUser => {
sessionStorage.setItem("email", email);
window.alert("You Are Successfully Signed In! Welcome " + email);
window.location = "homepage.html";
});
}, function(error) {
// Handle Errors here.
var errorMessage = error.message;
window.alert(errorMessage);
document.getElementById("txtPassword").value = "";
});
.catch()
方法仅在承诺被拒绝时运行。如果希望在承诺解析或被拒绝时运行代码,则应使用.then()
。它有两个函数参数,一个表示成功,另一个表示拒绝
firebase.auth().signInWithEmailAndPassword(email, password)
.then(function() {
// Handle success here
firebase.auth().onAuthStateChanged(frebaseUser => {
sessionStorage.setItem("email", email);
window.alert("You Are Successfully Signed In! Welcome " + email);
window.location = "homepage.html";
});
}, function(error) {
// Handle Errors here.
var errorMessage = error.message;
window.alert(errorMessage);
document.getElementById("txtPassword").value = "";
});
可能永远不会满足else条件。
console.log(errorCode)
显示什么?@Barmar如果登录信息不正确(即电子邮件或密码不匹配),则它将显示其中一条正确的错误消息(auth.error-password、auth/invalid email等),即if条件中列出的错误消息。如果正确,则为nothing。@PritamBanerjee如果else条件从未满足,则应满足if语句否?据我所知,如果不满足if条件,那么它将执行else条件。(如果这让人困惑,请道歉).catch()
函数仅在promise报告错误时运行。如果登录成功,它为什么会报告错误?可能是else条件从未满足。console.log(errorCode)
show是什么?@Barmar如果登录信息不正确(即电子邮件或密码不匹配),它将显示一条正确的错误消息(auth.error-password、auth/invalid email等)if条件中列出的条件。如果正确,则为nothing。@PritamBanerjee如果else条件从未满足,则应满足if语句否?据我所知,如果不满足if条件,那么它将执行else条件。(如果这让人困惑,请道歉).catch()
函数仅在promise报告错误时运行。如果登录成功,为什么会报告错误?