Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[๐Ÿš€ Dev Log] ๋ฐ•์†Œ์˜ / 2022-06-11 #112

Open
2 tasks
soyoung931014 opened this issue Jun 11, 2022 · 0 comments
Open
2 tasks

[๐Ÿš€ Dev Log] ๋ฐ•์†Œ์˜ / 2022-06-11 #112

soyoung931014 opened this issue Jun 11, 2022 · 0 comments
Assignees

Comments

@soyoung931014
Copy link
Owner

์˜ค๋Š˜์€ ์–ด๋–ป๊ฒŒ ํ”„๋กœ์ ํŠธ์— ๊ธฐ์—ฌํ–ˆ๋‚˜์š”?

  • ๋ฐฑ์—”๋“œ comment RUD ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

์˜ค๋Š˜์˜ ํ”„๋กœ์ ํŠธ์—์„œ ๊ธฐ์–ตํ• ๋งŒํ•œ ๋ถ€๋ถ„์ด ์žˆ๋‚˜์š”?

๋น„๋™๊ธฐ ํ•จ์ˆ˜ ์•ˆ์—์„œ map๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฐฐ์—ด ๊ฐ’์„ ์ˆœํšŒํ•˜๋ คํ–ˆ๋”๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-11 แ„‹แ…ฉแ„’แ…ฎ 10 49 20

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

 async getBoardComments(id: number) {
    try {
      const comments = await this.commentRepository.find({ board_id: id });
      if (comments) {
        const user = comments.map( async (el) => {
            return await this.userRepository.findOne({ id: el.user_id });
          }),
        );
        console.log(user, 'user');
      }
    } catch (error) {
      return error;
    }

์ฐพ์•„๋ณด๋‹ˆ ์ฝœ๋ฐฑํ•จ์ˆ˜์— async๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด , Array.map์€ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ Promise์˜ ๋ฐฐ์—ด์„ returnํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. pending์ƒํƒœ๋Š” resolve ์ƒํƒœ๋„ ์•„๋‹ˆ๊ณ  reject ์ƒํƒœ๋„ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Promise.all()์„ ์ ์šฉ์‹œ์ผœ์ฃผ๋ฉด resolve๋œ ๊ฐ’๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

ํ—ค๊ฒฐ: Promise.all()์ ์šฉ

 async getBoardComments(id: number) {
    try {
      const comments = await this.commentRepository.find({ board_id: id });
      if (comments) {
        const user = await Promise.all(
          comments.map( async(el) => {
            return await this.userRepository.findOne({ id: el.user_id });
          }),
        );
        console.log(user, 'user');
      }
    } catch (error) {
      return error;
    }

์ •์ƒ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-11 แ„‹แ…ฉแ„’แ…ฎ 11 06 14

๋‚ด์ผ์€ ํ”„๋กœ์ ํŠธ์— ๊ธฐ์—ฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฌด์—‡์„ ํ•ด์•ผ ํ•˜๋‚˜์š”?

  • comment ํ”„๋ก ํŠธ
  • ๋„ค๋ธŒ ์ˆ˜์ •
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant