【发布时间】:2017-03-06 04:27:18
【问题描述】:
我目前正在尝试以 react native 从 firebase 数据库返回用户名。 console.log 在函数内部工作,但不记录函数外部的任何内容。我不知道我哪里错了。这是我的代码。
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
})
}
【问题讨论】:
-
这是预期的行为:回调内部的代码运行(可能很多)晚于它之后的代码。在这里查看我的解释:stackoverflow.com/questions/35419303/…
标签: javascript firebase react-native firebase-realtime-database