【发布时间】:2017-01-20 14:24:36
【问题描述】:
我是 Firebase 的新手,并且将它与 React Native 一起使用。我正在尝试使用 Firebase 中的数据填充 ListView。我看到我现在可以在 Firebase 中使用 Promises,但是,我似乎无法将它们与 .on() 一起使用,而只能与 .once 一起使用。因为需要监听变化,所以需要使用.on()。
当我想确保在从 Firebase 检索数据之后(且仅在之后)设置状态时,我面临的挑战就来了。我的代码如下:
listen() {
var self = this
var currentUserID = firebase.auth().currentUser.uid
var postList = firebase.database().ref('/users/'+currentUserID+'/postKeys')
postList.on('value', function(snapshot) {
var posts = []
snapshot.forEach((child)=> {
var postID = child.val().postID
var postRef = firebase.database().ref('posts/'+postID)
postRef.orderByChild('sortedDateInMilliseconds').on('value', function(postSnap) {
posts.push(postSnap.val())
})
})
self.setState({
postCount:posts.length,
dataSource:self.state.dataSource.cloneWithRows(posts)
})
})
}
结果是我最初得到一个空视图,因为在查询填充帖子数组之前调用了render()。有谁知道如何确保在查询填充数组后调用self.setState({})?
非常感谢您的帮助。
【问题讨论】:
标签: javascript firebase react-native promise firebase-realtime-database