inflearn logo
강의

Course

Instructor

[Code Camp] Highly concentrated backend course created at boot camp

save 파라미터에 스프레드 안쓰고 객체를 넘겨도 되나요?

Resolved

476

ZZAMBA

12 asked

0

 const savedProductSaleslocation = await this.productSaleslocationRepository.save({
  ...productSaleslocation,
});

위 코드에서, 아래처럼 코드를 바꿔봤습니다.

const savedProductSaleslocation = await this.productSaleslocationRepository.save(productSaleslocation);

정상 작동했는데 차이가 무엇인가요? 또 권장하는 방식은 무엇인가요?

tdd node.js nodejs javascript docker express nestjs rest-api NestJS

Answer 1

0

jini

안녕하세요 ZZAMBA님!

현재 로직(productSaleslocation)에서는 save 메서드에 하나의 인자만 전달하고 있기 때문에 아래 작성해주신 로직처럼 작성하여도 큰 문제가 없으나, save메서드에 여러개의 인자가 이용된다면 강의에서 진행하는 방식을 이용하여 작성해 주셔야 합니다.

product를 저장하는 로직과 비교하여 학습해 보시기 바랍니다. 감사합니다 :)

0

ZZAMBA

전 주차(typeorm-crud)에 배웠던 ProductService 내 create 메서드도 아래 코드처럼 객체를 직접 넣어주었고, 실행해본 결과, graphQL playground에서도 성공적으로 실행 되었습니다.

async create({ createProductInput }: { createProductInput: CreateProductInput }) {
    const result = await this.productRepository.save(createProductInput);
    return result;
  }

객체를 새로 생성하는 것과 그냥 원래 객체를 넣는 것의 차이만 있는 것인가요?

0

jini

안녕하세요 ZZAMBA님!

save메서드에서 아래의 경우처럼 하나의 인자를 전달해주는 경우에는 기존의 객체를 넣어주어도 크게 문제가 발생하지 않습니다.

async create({ createProductInput }: { createProductInput: CreateProductInput }) { 
      const result = await this.productRepository.save(createProductInput); 
      return result; 
}

아래처럼 여러개의 인자를 전달해주는 경우를 위해 스프레드 연산자를 사용하는 것입니다.

const result2 = await this.productRepository.save({ 
     ...product, 
     productSaleslocation: result, 
     productCategory: { id: productCategoryId },
 });


해당 내용은 1 : N 관계 등록 API 수업을 통해 자세하게 학습할 수 있으니 참고해 주시기 바랍니다. 감사합니다!

그래프 ql 문서 사용할때 느낌표 남는거 어떻게 없애나요?

0

83

2

강의 전체 소스 코드를 받고싶습니다.

0

76

2

fontawesome 사용 문의

0

77

2

소스 코드 부탁드립니다~

0

85

2

깃 레포지터리 소스

0

82

2

커리큘럼12.css 정렬 에 나오는 과제 정답알고싶어요

0

73

2

10-01 Entity TypeOrmModule.forRoot 에 entities

0

85

3

강의 버전관련 문의입니다

0

102

2

Ubuntu 설치 관련

0

61

1

schema.gql 질문 드립니다.

0

51

1

서버 재실행시 Many to Many

0

101

3

input 관련 문의

0

90

2

Rest API 보다는 graphql이 주된 내용인데

0

130

2

강의 전체 소스코드 받을수있을까요?

0

155

1

도커볼륨 마운트 관련

0

127

2

findOne 타입스크립트오류

0

109

1

http => htrtps 호출 인증서 신뢰 오류

0

351

1

self-signed certificate in certificate chain 에러 발생

0

413

1

mongoose 설치 오류

0

142

1

특정 API, 특정 IP 허용 (단일경로에 CORS 활성화)

0

281

2

08-06

0

178

3

구조랑 패턴 관련해서 질문

0

125

2

mydocker

0

128

2

coolsms statuscode 2000 인데 전송안돼는 경우 확인.

0

156

1