sequelize/ 로 받아온 데이터 키, 값 지우기

flobeeee·2021년 6월 3일
0

시행착오

목록 보기
14/43
post-thumbnail

문제발생

왼쪽이 출력값인데, id 와 themeData 를 지우고 싶었다.

시도

175 코드처럼 지우고 싶었으나 안먹힘.
highlight.dataValue 로 접근하고 싶었으나 ts 가 getDataValue 쓰라고 에러를 띄웠다.

해결방법

sequelize 로 가져온 데이터베이스 정보는 일반 객체가 아니라서
json 으로 변경한 뒤에 삭제하였다.

const data = highlightDB.map((highlight) => {
        highlight.setDataValue('highlightId', highlight.getDataValue('id'));
        highlight.setDataValue(
          'colorHex',
          highlight.getDataValue('themeData').getDataValue('colorHex'),
        );
        const json = highlight.toJSON(); // json 으로 변경하고
        delete json['themeData']; // 지우고
        delete json['id']; // 지우고
        return json; // 리턴
      });
      return data;
profile
기록하는 백엔드 개발자

0개의 댓글