2017-01-05 2 views
0

이 함수의 else이 실행되지 않고 if의 이유는 무엇입니까? 또한 코드가 느리게 실행되는 무언가가 있습니까? 아래는 제 코드입니다.IF 함수가 실행되지 ELSE가 아닌

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"; 
      }); 
     } 

    }); 
} 
+0

조건이 충족되지 않을 수 있습니다. –

+1

'console.log (errorCode)'는 무엇을 보여줍니까? – Barmar

+0

@Barmar 로그인 정보가 올바르지 않으면 (예 : 전자 메일 또는 암호가 일치하지 않는 경우) 올바른 오류 메시지 (auth.wrong-password, auth/invalid-email 등) 중 하나가 표시됩니다 if 조건. 그것의 정확한 그 후에 아무것도. – SpiderMonkey

답변

3

.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 = ""; 
    }); 
관련 문제