Node.js mongoose

이건선·2023년 3월 6일
0

문제점
다른 스키마로 작성된 값을 가져와서 배열로 넣어야했다.

해결


...

const posts = await Post.find();
  const rename = await Promise.all(
    posts.map(async (ele) => {
      const comments = await Comment.find({ postId: ele.postId });
      return {
        postId: ele.postId,
        userId: ele.userId,
        nickname: ele.nickname,
        title: ele.title,
        createdAt: ele.createdAt,
        comment: comments.length ? comments : [],
      };
    })
  );

...

알게된점
awaite Promise.all(...)을 사용해서 .map메서드 내부의 모든 비동기 작업의 완료를 기다린다.

profile
멋지게 기록하자

0개의 댓글