0

Issue: After it enters the firebase listener, the code works until this.state.users appears, where it stops working.

This part of code is located somewhere into a button but there is no problem with it.

this.setState({
  users:
    [
      {
        name: Object.values(snapshot.val())[i].name,
        email: Object.entries(snapshot.val())[i][0]
      }
    ],
  found:true
})

Additional information:

constructor() {
    super();
    this.state = { 
      searchInput: '',
      foundUsersOutput:null,
      users:[], <---
      found:false, <---
    }
    this.findUser = this.findUser.bind(this)
  }

This is where the problem is located, from here the error starts. The error refers to console.log(this.state.users+'2')

componentDidUpdate(){
  console.log(this.state.users+'0')
  console.log('here0')
  if(this.state.found === true){
    console.log(this.state.users+"1")
    database().ref('users').child(this.state.users[0].email).on('child_added', function(data){
      console.log('asd')
      console.log(this.state.users+'2')
    })
  }
}

Output:

[object Object]0
here0
[object Object]1
asd

Error: error part 1 error part 2

Grecu Alex
  • 19
  • 7
  • The `function` that you pass to the `on` method will not be called with `this` bound to whatever `this` was in `componentDidUpdate`. If you want `this` to remain the same within that callback function, then use an arrow function, not `function`. NB: this is a duplicate question. – trincot Jun 24 '20 at 15:46

0 Answers0