teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

解決しました

2020/10/16 10:23

投稿

cypherys
cypherys

スコア1

title CHANGED
File without changes
body CHANGED
@@ -47,6 +47,34 @@
47
47
 
48
48
  としてみたりしましたがそもそも使い方が間違ってるでしょうか。。。
49
49
 
50
- ### 補足情報FW/ツールのバージョンなど
50
+ ### 追記解決しました。
51
51
 
52
+ 回答いただいた方法を参考に実装できたように思っていたコードでは非同期の完了が待てていませんでした。
53
+ 試行錯誤の後、下記コードでうまく動くようになりました!
54
+
55
+ ```
56
+ export const fetchPosts = (docIDs) => {
57
+ return async (dispatch) => {
58
+ // PostsコレクションからPost_idでデータ取得
59
+ if (docIDs !== undefined) {
52
- ここにより詳細な情報を記載してください。
60
+ const postlist = []
61
+ const fetchPost = docIDs.map(async (postId) => {
62
+ await postsRef.doc(postId).get()
63
+ .then((snapshot) => {
64
+ const data = snapshot.data()
65
+ postlist.push(data)
66
+ })
67
+ return postlist
68
+ });
69
+
70
+ const arraylength = fetchPost.length - 1;
71
+ fetchPost[arraylength].then((result) => {
72
+ result.sort((a, b) => {
73
+ if(a.created_at > b.created_at) return -1;
74
+ if(a.created_at < b.created_at) return 1;
75
+ return 0;
76
+ })
77
+ dispatch(fetchPostsAction(result))
78
+ })
79
+ }
80
+ ```