hmk run dev

무한스크롤 본문

React

무한스크롤

hmk run dev 2021. 3. 29. 11:50

파이어 스토어 데이터를 짤라서 가져와야한다

정렬은 시간 데이터 내림차순!

 

 

const getPostFB = () => {
  return function (dispatch, getState, { history }) {
    const postDB = firestore.collection("post");  

    let query = postDB.orderBy("insert_dt", "desc").limit(2); //콜랙션에서 내림차순으로 데이터 가져오기

    query.get().then((docs) => {
      let post_list = [];

      docs.forEach((doc) => {
        // 잘 가져왔나 확인하기! :)
        // 앗! DB에서 가져온 것하고 우리가 Post 컴포넌트에서 쓰는 데이터 모양새가 다르네요!
        // console.log(doc.id, doc.data());

        // 데이터 모양을 맞춰주자!
        let _post = doc.data();
        // ['comment_cnt', 'contents', ....]
        let post = Object.keys(_post).reduce(
          (acc, cur) => {
            if (cur.indexOf("user_") !== -1) {
              // user_가 포함이 안돼 있다면
              return {
                ...acc,
                user_info: { ...acc.user_info, [cur]: _post[cur] },
              };
            }
            return { ...acc, [cur]: _post[cur] };
          },
          { id: doc.id, user_info: {} }
        );

        post_list.push(post);
      });

      // 리스트 확인하기!
      console.log(post_list);

      dispatch(setPost(post_list));
    });

    return;

 

 

Comments