문제
TypeError: Cannot read properties of undefined (reading 'name')
74 | }
75 | static associate() {
> 76 | User.hasMany(Box_1.default, {
| ^
77 | sourceKey: "id",
78 | foreignKey: "UserId"
79 | });
Sequelize가 일반 서버 환경에서는 멀쩡히 동작했지만 Jest 테스트를 실행하면 다음과 같은 오류를 내보냈다.
해결
검색 결과 Box_1.default 객체가 undefined인 상태로 전달되어 생긴 문제일 가능성이 있다는 것을 알게 되었다.
모듈이 import되었는지 default export가 "name" property를 가진 객체인지 확인해보아야 한다.
user.ts로 들어가 Box_1의 import를 확인해 보니
const Box_1 = __importDefault(require("./Box."));
의 형태로 Box.js가 아닌 그냥 Box를 import 해오고 있기 때문에 오류가 발생했었다.
이전에 box.ts가 Box.js로 변환되는 문제가 있었지만 작은 생각해서 놔둔 것이 훗날 이렇게 거대한 문제가 되어 돌아왔다,
작은 문제라도 그대로 놔두면 안된다는 교훈을 깨닫는다...
tsc로 컨버트 하면 uppercase로 변환되는 오류는 tsconfig에 "forceConsistentCasingInFileNames": true를 추가하는 것으로 해결했다. 이전에 한번 Box.js로 컨버트 했던 것의 메타기록이 남이 있었던것 같다.
출처: https://stackoverflow.com/questions/36628612/typescript-transpiler-casing-issue
Typescript transpiler: casing issue
I have a typescript file that used to be called Group.ts. When transpiling to javascript the resulting file would be called Group.js. I've now renamed the typescript file to group.ts (lowercase g).
stackoverflow.com
오류 코드였던 at Function.hasMany (node_modules/sequelize/src/associations/mixin.js:34:25)에 들어가려 하니 src폴더가 없어 대신 lib에 들어가보았다. 주석으로 처리된 오류를 내는 경우가 전부 ...called with something that's not a subclass of Sequelize.Model로 나와있다.
User.hasMany(box_js_1.default, {
sourceKey: "id",
foreignKey: "UserId",
});
오류코드 의 box_js_1이 import되지 않아 undefined였던 것이 문제였는데 undefined는 당연하게 Sequelize.Model의 하위클래스가 아니기에 오류가 발생하였다.
'Trouble shooting' 카테고리의 다른 글
하나의 Nginx 서버에 여러 static page 배포 (Next.js) (0) | 2023.06.27 |
---|---|
React와 Node로 Github OAuth구현 (Typescript, Proxy server) (0) | 2023.01.22 |