Sequelize Association 오류
511
작성한 질문수 4
start로 시작한 서버에서는 sequelize가 정상 작동하는데 test를 하면 아래와 같은 오류가 발생합니다
FAIL tests/integration/intergration.test.ts
● Test suite failed to run
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 | });
at new HasMany (node_modules/sequelize/src/associations/has-many.js:51:37)
at Function.hasMany (node_modules/sequelize/src/associations/mixin.js:34:25)
at Function.associate (models/user.js:76:14)
at Object.<anonymous> (models/index.js:26:19)
at Object.<anonymous> (tests/integration/intergration.test.ts:2:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.43 s
Ran all test suites.sequelize 공식 문서를 보고 그대로 적용해 고쳐보았지만 무엇이 문제인지 잘 모르겠습니다 stackoverflow에서는 sequelize의 오류라고 하는 것 같은데 현재 저의 수준에서는 대화의 내용조차 잘 감이 잡히지 않습니다...
공식 문서를 보고 association들을 수정하면 TypeError: Cannot read properties of undefined (reading 'name') 에서 name이 UserId 등으로 바뀐 것도 몇번 보았지만 공식문서에서 나온대로 수정하면 다시 'name'을 읽을때 찾을 수 없다고 나옵니다. 또한
at new HasMany (node_modules/sequelize/src/associations/has-many.js:51:37)
at Function.hasMany (node_modules/sequelize/src/associations/mixin.js:34:25) 은 들어갈 수 없는 파일처럼 보입니다.
import Sequelize, {
CreationOptional,
InferAttributes,
InferCreationAttributes,
Model,
ForeignKey,
} from "sequelize";
import {
HasManyAddAssociationMixin,
HasManyCountAssociationsMixin,
HasManyCreateAssociationMixin,
HasManyGetAssociationsMixin,
HasManyHasAssociationMixin,
} from "sequelize/types/associations";
import Bookmark from "./bookmark.js";
import User from "./user.js";
class Box extends Model<InferAttributes<Box>, InferCreationAttributes<Box>> {
declare id: CreationOptional<number>;
declare box: string;
declare img: string;
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;
declare UserId: ForeignKey<User["id"]>;
declare getBookmarks: HasManyGetAssociationsMixin<Bookmark>;
declare addBookmarks: HasManyAddAssociationMixin<Bookmark, number>;
declare hasBookmarks: HasManyHasAssociationMixin<Bookmark, number>;
declare countBookmarks: HasManyCountAssociationsMixin;
declare createBookmarks: HasManyCreateAssociationMixin<Bookmark>;
static initiate(sequelize: Sequelize.Sequelize) {
Box.init(
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
box: {
type: Sequelize.STRING(15),
allowNull: false,
},
img: {
type: Sequelize.STRING(200),
allowNull: true,
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
},
{
sequelize,
timestamps: true,
underscored: false,
modelName: "Box",
tableName: "boxs",
paranoid: false,
charset: "utf8mb4",
collate: "utf8mb4_general_ci",
}
);
}
static associate() {
Box.belongsTo(User, { targetKey: "id" });
Box.hasMany(Bookmark, {
sourceKey: "id",
foreignKey: "BoxId",
});
}
}
export default Box;box.ts입니다
import Sequelize, {
CreationOptional,
InferAttributes,
InferCreationAttributes,
Model,
} from "sequelize";
import {
HasManyAddAssociationMixin,
HasManyCountAssociationsMixin,
HasManyCreateAssociationMixin,
HasManyGetAssociationsMixin,
HasManyHasAssociationMixin,
} from "sequelize/types/associations";
import Box from "./Box";
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
declare id: CreationOptional<number>;
// 'CreationOptional' is a special type that marks the field as optional
// id can be undefined during creation when using `autoIncrement`
declare email: string;
declare nick: string;
declare password: CreationOptional<string>;
declare provider: CreationOptional<string>;
declare snsId: CreationOptional<string>;
declare createdAt: CreationOptional<Date>;
// createdAt can be undefined during creation
declare updatedAt: CreationOptional<Date>;
// updatedAt can be undefined during creation
declare deletedAt: CreationOptional<Date>;
// ...
declare getBoxs: HasManyGetAssociationsMixin<Box>;
declare addBoxs: HasManyAddAssociationMixin<Box, number>;
declare hasBoxs: HasManyHasAssociationMixin<Box, number>;
declare countBoxs: HasManyCountAssociationsMixin;
declare createBoxs: HasManyCreateAssociationMixin<Box>;
static initiate(sequelize: Sequelize.Sequelize) {
User.init(
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email: {
type: Sequelize.STRING(40),
allowNull: true,
unique: true,
},
nick: {
type: Sequelize.STRING(15),
allowNull: false,
},
password: {
type: Sequelize.STRING(100),
allowNull: true,
},
provider: {
type: Sequelize.ENUM("local", "kakao", "github"),
allowNull: false,
defaultValue: "local",
},
snsId: {
type: Sequelize.STRING(30),
allowNull: true,
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
deletedAt: Sequelize.DATE,
},
{
sequelize,
timestamps: true,
underscored: false,
modelName: "User",
tableName: "users",
paranoid: true,
charset: "utf8",
collate: "utf8_general_ci",
}
);
}
static associate() {
User.hasMany(Box, {
sourceKey: "id",
foreignKey: "UserId"
});
}
}
export default User;
user.ts입니다
답변 2
0
association 오류가 아니라 tsc로 컨버트된 const Box_1 = __importDefault(require("./Box."));에서 "./Box.js"를 불러오는게 아니었기 때문에 오류가 발생했습니다.
chatGPT한테 몇가지 질문을 던져봤는데 성능이 엄청나네요 답변은 다음과 같습니다.
--
Error message인 "TypeError: Cannot read properties of undefined (reading 'name')" 는 그 코드에서 "name" property를 접근하려고 할 때 undefined object를 접근하려고 할 때 발생한다는 것을 의미합니다.
User.hasMany 메소드 호출이 발생한 코드인 models/user.js 파일의 associate 함수에서 Box_1.default 객체가 undefined 상태로 전달되어서 생긴 문제 같습니다. Box_1 모듈이 제대로 import 되었고, default export가 "name" property를 가진 object 인지 확인해주세요.
--
이전에 질문했던 내용도 그렇고 box.ts만 Box같이 대문자로 변형되고 뒤에 .js도 안붙는데 혹시 이게 어떤 원인에서 발생하는 오류인지 아시나요?
0
https://stackoverflow.com/questions/36628612/typescript-transpiler-casing-issue
이전에 Box한번 ts를 컨버트 했던게 문제였던것 같습니다.
tsconfig에서 "forceConsistentCasingInFileNames": true로 설정하고 문제 해결했습니다 답변 갑사합니다!!
--
문제 해결하기 전까지는 지우고 다시 생성해도 똑같이 uppercase로 컨버트 되어서 나왔었습니다
0
at new HasMany (node_modules/sequelize/src/associations/has-many.js:51:37)
at Function.hasMany (node_modules/sequelize/src/associations/mixin.js:34:25)
여기에 들어갈 수 있습니다. 저 경로로 직접 들어가시면 됩니다. 해당 name이 어떤 객체의 속성인지 파악하는게 중요합니다.
'S3' 형식에 'S3Client' 형식의 destroy, middlewareStack, send 속성이 없습니다.ts(2739)
0
509
1
throw new Error(&#x60;${this.name}.hasMany called with something that&#x27;s not a subclass of Sequelize.Model&#x60;); 에러 질문
0
343
1
안녕하세요.. connect ECONNREFUSED 127.0.0.1:3306 관련해서 질문드립니다
0
958
1
@types를 dependencies에 넣는 이유?
0
401
1
JS에서 babel 사용시 `import * as` 구문을 안써도되는데, 바벨이 esModuleInterop: true 로 처리해주는 것인가요?
0
318
1
Sequelize constructor.primaryKeyAttributes 오류
0
973
1
Sequelize에 요청을 보내면 오류가 뜹니다
0
796
1
강좌에서 사용된 3개의 코드 의미가 궁금합니다 [ 코드 : 1. [ key:string]:string] 2. delete user.password , 3.passport.deserializeUser<number> ]
0
338
1
@mui/x-data-grid 에 사용자정의 컬럼 타입을 추가해서 사용하려고 합니다.
0
2619
1
미들웨어 에러
1
312
1
Could not find a declaration file for mould '../utils/jwt-utils'
1
180
1
마지막에 type과 interface 추가하는 부분
0
217
1
다른 컴퓨터에서 제가 만든 DB 테이블의 데이터를 그대로 사용하려면?
0
282
1
express 모듈 에러 관련
0
239
1
import * as A from 'B'
0
215
1
MySql ssl 보안 옵션 질문 입니다.
0
353
2
passport/index.ts 에러 입니다
1
452
1
routes/post.ts 페이지에서 에러나요..
0
314
1
passport/index.ts 에러납니다...
0
817
1
ts import 에러
0
442
1
타입스크립트로 변환후 nextjs 빌드 후 배포
0
262
1
sequelize include 질문드립니다.!
0
191
1
roperty 'id' does not exist on type 'User'.
0
898
1
passport user.id 질문드립니다.
0
204
1





