(RESOLVED) Assossiate 2 objects Firebase Ionic 3


#1

Hi guys, I’ve trying assossiate 2 objects in Firebase in the Ionic, first I call one method in service and after I call second method in another service, look this error

  constructor( 
    private chatService: ChatProvider,
    private userService: UserProvider) { 
  }


getUsers() {
    this.userService.getUsers('citizens')
        .subscribe((res) => {
          this.citizens = res;
           this.final_data = this.citizens.map(function(citizen) { 
            return this.chatService.getMessage(citizen.key); 
          })
          console.log(this.final_data)
        })
  }

error: core.js:1448 ERROR TypeError: Cannot read property ‘chatService’ of undefined

Someone ?


#2

Change:

this.final_data = this.citizens.map(function(citizen) { 
    return this.chatService.getMessage(citizen.key); 
})

to:

this.final_data = this.citizens.map((citizen) => { 
    return this.chatService.getMessage(citizen.key); 
})

That is, use arrow functions, so that this is correctly scoped to your class, instead of being in the scope of the function:


#3

Now work my friend thanks