我在从firebase特定位置检索数据时遇到问题。我有一个州("收藏夹"取自firebase)。收藏夹状态对象仅包含在单击&#34时推送的唯一firebase键;发布添加到收藏夹" (我在firebase中发布了数据)。那么,我怎么能用那些在"收藏夹"中的唯一键检索多个帖子数据?节点
代码:
class MyFavouriteScreen extends Component {
constructor(props) {
super(props);
this.state = {
favourites: [],
favouritesPosts: [],
};
}
componentWillMount() {
let user = firebase.auth().currentUser.uid;
favouritesFire = firebase.database().ref('users').child(user + '/favourites');
let that = this;
favouritesFire.once("value", function(snapshot){
let favs = [];
snapshot.forEach(function(data){
let fav = {
id: data.key
}
favs.push(fav);
that.setState({favourites: favs});
});
});
// ^- there i get those unique keys and now i need to retrieve post data with those keys and show them in render function
}
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.mainContainer}>
<MyNavScreen banner="Favoritai" navigation={ navigate } />
<ScrollView style={styles.wrapper}>
{this.state.favouritesPosts.map(post => {
return (
<TouchableOpacity post={post} style={styles.postWrapper}>
<View style={styles.postImageWrapper}>
<Image
source={{uri: post.image}}
style={{flex: 1}} />
</View>
<View style={styles.shopContent}>
<Text style={styles.postTitle}>{post.title}</Text>
<Text style={styles.postText}>{post.text}</Text>
</View>
<TouchableOpacity style={styles.starWrapper}>
<Text style={styles.star}>{'-'}</Text>
</TouchableOpacity>
</TouchableOpacity>
);
})}
</ScrollView>
</View>
);
}
}
答案 0 :(得分:0)
Firebase处理查询并不是那么好。我建议你这样的事情
favouritesFire.once("value", snapshot => {
snapshot.map(item => { // it will pass through all your snapshot items
firebase.database().ref(`yournode/${item.key}`) //for each item you must query again in firebase
.once('value')
.then(itemFiltered => console.log('Your item: ', itemFiltered); // then you get your result
})
})
希望有所帮助