2017-03-08 2 views
1

저는 데이터베이스로 Firebase v3를 사용하는 이온 2 앱에서 작업하고 있습니다. 내 기능은 이미 작동하지만, 나는 그것이 나에게이 오류주는 무슨 잘못 사용자에게 간단한 경고를 표시하려고하면 다음 null의 'alertCtrl'속성을 읽을 수 없습니다.

Cannot read property 'alertCtrl' of null 

내 함수의 :

updateUniqueCode(uniqueCode: string): any { 

    this.userProfile.on("child_added", function(snapshot,prevChildKey){ 
     let allUniqueCodes = snapshot.val(); 

     if (allUniqueCodes.uniqueCode === uniqueCode){ 

     let alert = this.alertCtrl.create({ 
          message: "no", 
          buttons: [{ text: "Ok"} ] 
         }); 

         alert.present(); 



     } else { 

     let currentUser = firebase.auth().currentUser; 
     let userProfile = firebase.database().ref('/userProfile'); 

      return userProfile.child(currentUser.uid).update({ 
      uniqueCode:uniqueCode 
      }) 
     } 
    }); 
    } 

내가 많이 시도했습니다를 다른 솔루션으로,하지만 아무것도 미리 :( 감사를 작동하는 것 같다되지

답변

1

당신이 함수의 내부에있어 때문에 (이 기능 : function(snapshot,prevChildKey){ ... })하세요 this 키워드가 더 이상 구성 요소에 대한 참조하지 지내에 의해보십시오. 이 같은 g을 :

updateUniqueCode(uniqueCode: string): any { 

    this.userProfile.on("child_added", (snapshot,prevChildKey) => { 
     let allUniqueCodes = snapshot.val(); 

     if (allUniqueCodes.uniqueCode === uniqueCode){ 

     let alert = this.alertCtrl.create({ 
          message: "no", 
          buttons: [{ text: "Ok"} ] 
         }); 

         alert.present(); 



     } else { 

     let currentUser = firebase.auth().currentUser; 
     let userProfile = firebase.database().ref('/userProfile'); 

      return userProfile.child(currentUser.uid).update({ 
      uniqueCode:uniqueCode 
      }) 
     } 
    }); 

} 

의 차이는 이제 화살표 기능과 ...

An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target.

+1

Thankss을 사용하고 있다는 것입니다! 그것은 지금 일한다! !! :디 –

관련 문제