2017-12-19 12 views
1

firebase固有の場所からデータを取得する際に問題があります。私は状態(firebaseから取られた "お気に入り")を持っています。お気に入りの状態オブジェクトには、「お気に入りに追加する」をクリックするとプッシュされた固有のfirebaseキーのみが含まれています(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

あなたのコードとデータ構造は何ですがお役に立てば幸いですか! – Ben

+0

返信用のちょっとベンタンプ。私のコードを追加しました – MGalmostcoder

+0

@Ben助けてもらえますか? – MGalmostcoder

答えて

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 
    }) 
}) 

のようなものをお勧めしたい、それが

関連する問題