I am currently trying to return the username from the firebase database in react native. The console.log works inside the function but doesn't log anything outside of the function. I don't know where I am going wrong. Here is my code.
componentWillMount() {
firebase.database().ref('/users/' + this.userId + '/username').once('value').then(function(snapshot) {
this.username = snapshot.val();
console.log(this.username)
});
console.log(this.username)
this.setState({
username: this.username
})
}