質問編集履歴
1
解決しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -47,6 +47,34 @@
|
|
47
47
|
|
48
48
|
としてみたりしましたがそもそも使い方が間違ってるでしょうか。。。
|
49
49
|
|
50
|
-
###
|
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
|
+
```
|