inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

탄탄한 백엔드 NestJS, 기초부터 심화까지

JWT와 로그인 서비스 & 순환 참조 모듈

async/await 질문

335

노른자

작성한 질문수 81

1

async / await을 만약 레포지토리에서 사용했다면

서비스에서는 레포지토리 함수를 async/await 없이 그냥 호출하면 안되나요?

예를 들어)

cat.repository.ts 파일에

import { createCatDto } from './dto/cat.create.dto';
import { ConflictException } from '@nestjs/common';
import { Cats } from 'src/entities/cat.entity';
import { EntityRepository, Repository } from 'typeorm';
import * as bcrypt from 'bcrypt';

@EntityRepository(Cats)
export class CatRepository extends Repository<Cats> {
async createCat(data: createCatDto) {
const theCat = await this.findOne({
where: {
email: data.email,
},
});
if (theCat) {
throw new ConflictException('이미 존재하는 고양이입니다.');
}
data.password = await bcrypt.hash(data.password, 10);
const newCat = await this.save(this.create(data));
delete newCat.password;
return newCat;
}

async findCatByEmail(email: string) {
const theCat = await this.findOne({
where: {
email,
},
});
return theCat;
}
}
라고 하고 findCatByEmail을 사용한 AuthService에서는
 
auth.service.ts
 
const cat = await this.catRepository.findCatByEmail(email)
 
위의 cat 정의에 await을 붙혀 줘야하는건가요??
저는 이미 레포지토리단에서 프로미스로 값을 받았으니 await을 안붙혀도되는줄알았는데
안붙히니까
const isPasswordValidated = await bcrypt.compare(password, cat.password )
cat.password 의 password에 빨간줄 들어오면서 타입스크립트에서 잡아주네요
await 빼먹은것같다면서,..
왜 await을 붙혀 줘야하는거죠?

a mongodb express nodejs NestJS ssr

답변 1

0

윤상석

안녕하세요! 답변이 늦었습니다. ㅠㅠ

Promise 객체에 대해 비동기 처리를 해주기 위해서는 await 키워드를 붙여 작성해야 합니다.

타입스크립트(및 자바스크립트) async/await 문법을 참고해주세요!

https://ko.javascript.info/async-await

프로젝트 환경 세팅할 때 최신 노드 버젼을 사용하시는 분들은 참고하셔도 좋을 것 같아요~

2

101

1

DTO에 대한 질문

1

97

2

백엔드 MVC에서 View의 역할은 무엇인가요?

1

111

2

추가 업데이트 관련 건

0

105

2

nest js 버전문제

0

95

2

mongdb 스키마 공식 문서와 형태가 다른 이유 궁금합니다.

0

113

1

라인 끝에 에러 표시(eslint) 때문에 구글 찾아 보니.

0

83

1

전체 고양이 조회 라우터 중 error.message 오류

0

82

1

캡슐화 추가 설명 중 단일책임원칙 관련 질문

0

115

0

42강 고양이끼리 소통 댓글 구현 중 Schema hasn't been registered for model 'comments' 에러 해결

0

86

1

채팅 이슈

0

137

1

모듈이 더 이상 지원하지 않는답니다

0

216

1

오류가 있습니다

0

114

1

import 에서 오류가 납니다

0

131

1

이런 오류가 나옵니다

0

108

1

에러가 발생합니다

0

117

1

프론트 에러 뜨는데 수정 안해주시나요

0

168

1

emit() broadcast.emit() 질문있습니다

0

107

1

서버연결이 안됩니다.

1

408

1

[PM2][ERROR] Command not found

0

528

1

S3에 업로드까지는 성공했는데 사진이 나오지 않습니다.

0

254

1

error_code : Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339)

0

607

1

jwt를 따로 연습하고 있는데 env를 못읽는 것 같습니다.

0

330

2

Ec2로 안하시는 이유가 있을까요?

0

345

1