sequelize: Class constructor Model cannot be invoked without 'new' and how getter v4 work?
Hi all, I using v4 to defined my model as:
@Options({
sequelize,
tableName: 'V2_category',
timestamps: false
})
@Attributes({
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: DataTypes.STRING,
name_vi: DataTypes.STRING,
code: DataTypes.STRING,
type: DataTypes.STRING,
created_at: DataTypes.DATE,
updated_at: DataTypes.DATE,
})
export class V2Category extends Model {
get fullName() {
return this.name + '-' + this.name_vi;
}
set fullName(fullName) {
const names = fullName.split(' ');
this.name = names.pop();
this.name_vi = names.join(' ');
}
}
and i import in others:
import {V2Category as category} from '../models/V2_category';
my query :
findCategoryById (id) { return category.findOne({ where: { 'id': id } }); }
When run i got an error:
Unhandled rejection TypeError: Class constructor Model cannot be invoked without ‘new’
However, i add { where: { 'id': id }, raw: true }
my code working but i can’t use getter function fullName. Could you tell me where my mistake and how to fix it? Thanks
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 10
- Comments: 30 (5 by maintainers)
Links to this issue
Commits related to this issue
- fix for error "Class constructor Model cannot be invoked without 'new'" See: https://github.com/sequelize/sequelize/issues/7840#issuecomment-317212324 — committed to Palus-Somni-Team/shikicinema-server by Smarthard 4 years ago
- build: change ts target from es5 to ES2020 Code is executed in nodeJS, so we can raise the target version. Also, this patch prevent the sequelize error below: `Class constructor Model cannot be in... — committed to LuckyWindsck/myfontsns-backend by LuckyWindsck 3 years ago
- Adds vuex-orm and examples References: https://github.com/sequelize/sequelize/issues/7840#issuecomment-317212324 https://github.com/vuejs/vetur/issues/762#issuecomment-712246564 https://github.com/Dm... — committed to gervasiocaj/vuex-orm-template by gervasiocaj 3 years ago
In
typescript
case, my solution was to change target fromes5
toes6
In JavaScript, a class is a function.
I think it has something to do with the way babel transpiles the code. After I changed my
.babelrc
file to this:it works as expected.
Mine got solved via:
Im not using a transpiler and i have this error as well
Works for me! Thanks
Same issue here and my .babelrc is:
Any other suggestions to solve this?
It turns out that an ES6-class transpiled by babel can’t extend a real ES6 class (The
Sequelize.Model
ES6-class for example).So
class MyEntity extends Sequelize.Model { ... }
won’t work ifMyEntity
is transpiled into ES5.See https://stackoverflow.com/questions/36577683/babel-error-class-constructor-foo-cannot-be-invoked-without-new
The solution i used, was to use
es2015-node5
instead ofes2015
. But ONLY on the server, of course, not on the client - i.e. not when transpiling for the browser. So i didn’t put this in my .babelrc, but added it to the “build-server” command-line scripts in package.json instead:babel .... --no-babelrc --presets=es2015-node5,stage-0,react
.@juhaelee says:
I had the same error with typescript and it was fixed when I updated the tsconfig.json changing the es5 for es6
“compilerOptions”: { “target”: “es6”, /* Specify ECMAScript target version: ‘ES3’ (default), ‘ES5’, ‘ES2015’, ‘ES2016’, ‘ES2017’, ‘ES2018’, ‘ES2019’, ‘ES2020’, or ‘ESNEXT’. / “module”: “commonjs”, / Specify module code generation: ‘none’, ‘commonjs’, ‘amd’, ‘system’, ‘umd’, ‘es2015’, ‘es2020’, or ‘ESNext’. */
I resolved my issue. I used
require
instead ofsequelize.import
. 😓For anyone who comes to this and the reason was not transpiling. For me it was because I was exporting my models as classes, and the line from the generated models/index.js :
const model = sequelize['import'](path.join(__dirname, file))
was causing the error. Once I changed that line to:
let model = require(path.join(__dirname, file));
It was fixed
Turns out in my case, I had a previously created model in the models folder that was causing things to break:
This was preventing the file from being read correctly by sequelize and causing the error listed in this thread. Advice for others: Make sure all of your code inside of your models folder is valid and from examples.
I am having the same issue, changed the appropriate line, still no luck.
Thank for your reply. I used
ES6
fortarget
but it doesn’t work.