2017-01-20 2 views
0

投稿またはコメントが作成されると、ユーザー作成者はIDだけで追加されます。ユーザーが自分のアバターや名前などを更新することを決めたときに、私はこれまでに投稿したすべての投稿やコメントを見つけて更新する必要はありません。すべての投稿を取得する際に、各投稿の著者情報を取得するにはどうすればよいですか?

私は単に記事をつかみ、(著者情報なし)のペイロードとして、それらを通過した時、それがうまく働いていた

enter image description here

、エラーなし:

export const fetchPostsByNewest =() => { 
    return (dispatch) => { 
    firebase.database().ref('/social/posts') 
     .on('value', snapshot => { 
      dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: snapshot.val() }); 
     }; 
    }; 
}; 

私がしようとして試してみましたこのような著者情報を得るが、私はちょうど1つのペイロードにそれをすべて行う方法の解決策を考え出すことはできません。

export const fetchPostsByNewest =() => { 
    return (dispatch) => { 
    firebase.database().ref('/social/posts').on('value', postSnapshot => { 
     const posts = postSnapshot.val(); 

     const postsAsArray = Object.keys(posts).map(postId => posts[postId]); 
     postsAsArray.forEach(post => { 
      const postWithUser = {}; 

      Object.keys(post).forEach(key => { 
       postWithUser[key] = post[key]; 
      }); 

      const userId = post.author; 

      firebase.database().ref(`/social/users/${userId}`).once('value', userSnapshot => { 
       const profile_info = userSnapshot.val(); 

       postWithUser.profile_info = profile_info.profile_info; 

       console.log(postWithUser); 

       dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: postWithUser }); 
      }); 
     }); 
     }); 
    }; 
}; 

これらは私がABから取得していますコンソールログがありますOVE:

enter image description here

権利データを通じて持っているようだが、私はちょうどこのエラーを取得する:

enter image description here

あなたは私にいくつかの提案をお願いすることができ、これは狂気私を運転しています!

ありがとうございます!

答えて

1

約束を返しonce() Firebase APIによるので、あなたのコードは次のように多かれ少なかれになります。ここでは

firebase.database().ref(`/social/users/${userId}`).once('value').then(userSnapshot => { 
    const profile_info = userSnapshot.val(); 
    postWithUser.profile_info = profile_info.profile_info; 

    console.log(postWithUser); 

    dispatch({ type: NEW_POSTS_FETCH_SUCCESS, payload: postWithUser }); 
}); 

は、ドキュメントへの参照です:https://firebase.google.com/docs/reference/node/firebase.database.Reference#once

関連する問題