'(nuxt sequelize-typescript) Error:Cannot read properties of undefined (reading 'prototype')
Issue
when the models are added, the error Cannot read properties of undefined (reading 'prototype') happens.
Versions
- sequelize:6.19.0
- sequelize-typescript:2.1.3
- typescript:
below is the content of package.json
{
"dependencies": {
"nuxt": "^2.15.8",
"pg": "^8.7.3",
"pg-hstore": "^2.3.4",
"reflect-metadata": "^0.1.13",
"sequelize": "^6.19.0",
"sequelize-typescript": "^2.1.3",
"vue": "^2.6.14",
"webpack": "^4.46.0"
},
"devDependencies": {
"@nuxt/types": "^2.15.8",
"@nuxt/typescript-build": "^2.1.0",
"@types/node": "^17.0.24",
"@types/validator": "^13.7.2",
"sequelize-cli": "^6.4.1"
}
}
Actual behavior
the error happens when sequelize.addModels() is executed.
I attached the error message just after the related code.
Expected behavior
the error does not happen and models can be added.
Steps to reproduce
create companyMaster.ts like the code.
import companyMaster.ts in index.ts .
execute index.ts .
I referenced the link about how to add models.
Related code
commons/sequelize/models/companyMaster.ts
import { Table, Column, Model } from 'sequelize-typescript';
@Table({
schema: 'public',
tableName: 'company_master',
underscored: true,
timestamps: true,
})
export class CompanyMaster extends Model {
@Column
companyNumber!: string;
@Column
companyName!: string;
@Column
homepageUrl!: string;
}
index.ts
import { Sequelize } from 'sequelize-typescript';
import { CompanyMaster } from './commons/sequelize/models/companyMaster';
const sequelize = new Sequelize(process.env.DB_CONNECTION_URI || '', {
models: [__dirname + '/commons/sequelize/models'], // (edited) here also the error happens.
});
sequelize.addModels([CompanyMaster]); // the error happens here
the error message for the line sequelize.addModels([CompanyMaster]);
Cannot read properties of undefined (reading 'prototype')
at node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:62:71
at Array.map (<anonymous>)
at Sequelize.defineModels (node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:61:23)
at Sequelize.addModels (node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:37:36)
at server/index.ts:30:15
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
(edited) the error message for the line models: [__dirname + '/commons/sequelize/models'],
Cannot find module 'commons/sequelize/models/companyMaster'
Require stack:
- node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js
- node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js
- node_modules/sequelize-typescript/dist/index.js
- server/index.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename (node_modules/module-alias/index.js:49:29)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:45:32
at Array.map (<anonymous>)
at node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:43:18
at Array.reduce (<anonymous>)
at getModels (node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:35:20)
at Sequelize.addModels (node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:36:58)
at new Sequelize (node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:20:22)
at server/index.ts:24:23
at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
at next (node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (node_modules/express/lib/router/route.js:112:3)
Solution 1:[1]
If you will use the "models" options, you have to export the instance (CompanyMaster) class as default.
And this line:
models: [__dirname + '/commons/sequelize/models']
Must be:
models: [__dirname + "/commons/sequelize/models/*.ts"]
Also, you don't need ".addModels()"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
