So you're doing an async API request to Firebase.
In order to use logoUrl in your component methods, first you have to keep it in the state, once its value is returned from Firabase.
Your flow should be:
- Trigger the API request for getting the logo.
- When the response is returned, then you should keep it in your component state.
- When it's in the state, you can access it in your component.
Keep in mind that the initial value of logoUrl is null (or whatever value you set in the constructor), while the logo is being fetched from the API. When it's null, you can show a loading message.
Something like that:
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
logoUrl: null
}
}
getLogo() {
const userId = fire.auth().currentUser.uid
fire.database().ref('/clients/' + userId).once('value')
.then(function(snapshot) {
let companyID = (snapshot.val().companyID) || 'Anonymous'
fire.database().ref('/company/' + companyID)
.once('value')
.then(function(snapshot) {
const logoUrl = snapshot.val().logo
this.setState({
logoUrl
})
})
})
}
componentDidMount() {
this.getLogo()
}
render() {
return this.state.logoUrl ? <img src={this.state.logoUrl} /> : <div>Loading ...</div>
}
}
letas opposed tovar.