inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

따라하며 배우는 노드, 리액트 시리즈 - 레딧 사이트 만들기(NextJS)(Pages Router)

useSWR을 이용한 커뮤니티 리스트 가져오기

routes/subs.ts_topSubs 쿼리빌더 관련 질문 있습니다.

294

hi2177

작성한 질문수 1

0

useSWR을 이용한 커뮤니티 리스트 가져오기 강의에서

routes/subs.ts의 topSubs 함수를

const topSubs = async (req: Request, res: Response) => {
  try {
    const imageUrlExp = `COALESCE('s."imageUrn",'https://www.gravatar.com/avatar?d=mp&f=y')`;
    const subs = await AppDataSource.createQueryBuilder()
      .select(
        `s.title, s.name, ${imageUrlExp} as "imageUrl", count(p.id) as "postCount"`
      )
      .from(Sub, "s")
      .leftJoin(Post, "p", `s.name = p."subName"`)
      .groupBy('s.title, s.name, "imageUrl"')
      .orderBy(`"postCount"`, "DESC")
      .limit(5)
      .execute();
    return res.json(subs);
  } catch (error) {
    console.log(error);
    return res.status(500).json({ error: "문제가 발생했습니다." });
  }
};

으로 강사님이 작성해주셨는데, 저는 아래와 같이 해야 동작하더라구요.

const topSubs = async (req: Request, res: Response) => {
  try {
    const imageUrlExp = `COALESCE(s.imageUrn, 'https://www.gravatar.com/avatar?d=mp&f=y')`;
    const subs = await AppDataSource.createQueryBuilder()
      .select(`s.name, s.title, ${imageUrlExp} as "imageUrl", count(p.id) as "postCount"`)
      .from(Sub, 's')
      .leftJoin(Post, 'p', `s.name = p.subName`)
      .groupBy('s.name, s.title, "imageUrl"')
      .orderBy(`"postCount"`, 'DESC')
      .limit(5)
      .execute();
    return res.json(subs);
  } catch (error) {
    console.log(error);
    return res.status(500).json({ error: '문제가 발생했습니다.' });
  }
};

제가 postgresql이 아닌 mariadb를 사용하고 있는데, db가 달라서 생기는 차이가 맞나요? 아니면 다른 이유가 있는지 궁급합니다!

typescript docker postgresql nodejs react Next.js 클론코딩

답변 1

1

John Ahn

안녕하세요!

차이점을 보니 s.name = p.subName 이랑 s.name = p."subName" 차이네요!

이 부분에 대해서는 방금 공식문서에 들어가서 예제나 이에 대한 설명을 찾아봤는데 없네요 ㅠㅠ!!

그래도 생각해보면 orm이 orm 자체에서 정해준 쿼리문법을 이용하면 그걸 각각 디비에 맞게 (mariadb 면 mariadb를 위해서 oracle이면 오라클 디비를 위해서 mongoDB면 mongoDB를 위해서) 바꿔주는데 그 과정에서 약간의 문법 차이가 발생해서 에러가 발생하는 것 같습니다!

감사합니다!

0

hi2177

공식문서까지 찾아주시고,, 친절한 답변 감사합니다!

toJson을 추가하면 [sub].tsx에서 sub를 받아오지 못합니다.

0

122

2

쿠키 저장이 되지 않습니다.

0

226

1

AxiosError {message: 'Request failed with status code 401/500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', (2)

0

581

1

AxiosError {message: 'Request failed with status code 401/500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE',

0

651

1

overload 에러

0

170

1

docker compose up 오류

0

210

1

부록) remark 강의 중 parmas 오류

0

140

1

3000번은 잘 들어가지는데 80번은 안됩니다.

1

296

0

커뮤니티를 올리고 난 후 404 page

0

209

1

tailwind css 문제인지, className 에 적용한 css가 적용되지 않아요.

0

1043

2

tsx 수정 시 마다 빌드 후 서버 시작 해야하나요?

0

656

2

useState 쳤을 때 자동완성 되는 단축키 무엇인가요? extention 인가요?

0

805

2

리액트 서버 npm run dev 와 npm run build 후 npm start 의 차이

0

4615

2

data 폴더가 생성되지 않아요.

0

527

1

docker-compose up 오류

0

814

1

회원 가입 페이지 기능 생성(3) 중 에러

0

380

2

Entity에 toJSON 코드 입력 후 404 에러

0

252

1

context에서 useEffect 선언 부분 질문 있어요.

0

297

1

src 폴더구조

0

511

2

서버 실행 시 에러 관련하여 답변받고 1차 조치했는데 여전하여서 질문 남깁니다

0

298

1

엔티티 모두 작성 후 서버 실행 시 에러가 발생합니다

0

313

1

회원가입 누르면 404에러가 뜹니다 ;-;

1

424

1

nextjs버젼에 대해서 질문드립니다.

0

373

1

<npm run dev>시 -61 에러가 나타납니다!

0

359

1