2017-07-07 13 views
0

別のメソッドからメソッドを呼び出そうとしていますが、 "writeUserData"というエラーは定義されていません。 これは私のコードです:私のメソッドが定義されていません

export default class RegisterScreen extends Component{ 

    constructor(props){ 
    super(props) 
    this.state = { 
     email: '', 
     password: '', 
     verify: '', 
     nickname: '', 
    } 

    this.handlePress = this.handlePress.bind(this) 
    this.writeUserData = this.writeUserData.bind(this) 
    } 

    handlePress(navigation){ 
    if(this.state.password == this.state.verify){ 
     firebaseRef.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).then(function(newUser){ 
     const resetAction = NavigationActions.reset({ 
      index: 0, 
      actions: [ 
      NavigationActions.navigate({ routeName: 'Home'}) 
      ] 
     }) 

     navigation.dispatch(resetAction) 
     writeUserData(newUser.uid, this.nickname); //Problem here. 
     }).catch(function(error){ 
     console.log(error); 
     }); 
    }else{ 
     //password not match, show error. 
    } 
    } 

    writeUserData(mUID, mNickname){ 
    console.log("WRITE"); 
    firebaseRef.database().ref('users/' + mUID + '/').set({ 
     nickname: mNickname, 
    }); 
    } 
} 

私は、データベースへの登録後、ユーザーのニックネームとUIDの書き込みしようとしています。 私はFirebaseとReact Nativeを使用しています。

私もthis.writeUserDataを書き込もうとしましたが、「this.writeUserData」は関数ではないというエラーが表示されます。

私は間違っていますが、どうすれば修正できますか?

ありがとうございます!

答えて

2

あなたは矢印の機能を使用する場合、それはあなたがあなたの方法を使用できるようになるあなたのクラスへthisの参照に変更します:

handlePress = (navigation) => { //this also makes it so you dont have to bind in constructor 
    if(this.state.password == this.state.verify){ 
     firebaseRef.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).then((newUser) => { //arrow function here 
     const resetAction = NavigationActions.reset({ 
      index: 0, 
      actions: [ 
      NavigationActions.navigate({ routeName: 'Home'}) 
      ] 
     }) 

     navigation.dispatch(resetAction) 
     this.writeUserData(newUser.uid, this.nickname); //Problem here. 
     }).catch(function(error){ 
     console.log(error); 
     }); 
    }else{ 
     //password not match, show error. 
    } 
    } 
+0

をありがとう!この矢印機能の詳細はどこで知ることができますか? –

+0

矢印機能はes6です。詳しくはこちらをご覧ください:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions –

+0

ありがとう、ありがとうございました。 –

関連する問題