Sequelize-typescript: Class constructor Model cannot be invoked without 'new'

Created on 13 Jun 2017  路  3Comments  路  Source: RobinBuschmann/sequelize-typescript

Hi,
I'm trying to load the models and getting the error Class constructor Model cannot be invoked without 'new'

Here's the ./models/index.ts:

import {Sequelize} from 'sequelize-typescript';
import * as configEnv   from './../config/config.json';
import {User} from './user;

const env = process.env.NODE_ENV || 'development';
const sequelize_main =  new Sequelize(configEnv[env]);

sequelize_main.addModels([
      User
])

export const db = sequelize_main;

In controller ./routes/registration:

import {db} from './models';
import {User} from './models/user';
const models = db.models;
const router = express.Router();

router.get('/', (req, res, next)=>{

    let user = new User({
        email:'[email protected]'
    });
});

This way i'm getting the error Class constructor Model cannot be invoked without 'new'

Also I've tried another way and got the same error:

router.get('/', (req, res, next)=>{

    let user = models.User.build({
        email:'[email protected]'
    });
});

Is it a correct way to load and enable the sequelize models? So, the problem might be related with the environment

Thank you!

Most helpful comment

Have exactly the same issue. But when I set the target to es6 the sequelized-typescript module is not found anymore. @YaroslavOsetrov can you provide your tsconfig.json here?

Update:

This tsconfig.json works for me:
{ "compilerOptions": { "module": "commonjs", "target": "es6", "experimentalDecorators": true, "emitDecoratorMetadata": true, "sourceMap": true, "declaration": true, "strictNullChecks": true, "noUnusedLocals": true, "pretty": true, "skipLibCheck": true, "lib": [ "es2015" ] }, "sourceMap": true, "outDir": ".build", "moduleResolution": "node", "rootDir": "./" }

All 3 comments

Hey @YaroslavOsetrov, which sequelize version are you using?

Sequelize 4 uses es6 classes for its models. So if you are using sequelize 4, you need to set es6, es2015 es2016 or es2017 as the target in your tsconfig.json. This is because es5 constructor-pattern "classes" cannot extend es6 classes.

Hi @RobinBuschmann

Thank you!
Exactly, I'm using sequelize 4 with the es5

Have exactly the same issue. But when I set the target to es6 the sequelized-typescript module is not found anymore. @YaroslavOsetrov can you provide your tsconfig.json here?

Update:

This tsconfig.json works for me:
{ "compilerOptions": { "module": "commonjs", "target": "es6", "experimentalDecorators": true, "emitDecoratorMetadata": true, "sourceMap": true, "declaration": true, "strictNullChecks": true, "noUnusedLocals": true, "pretty": true, "skipLibCheck": true, "lib": [ "es2015" ] }, "sourceMap": true, "outDir": ".build", "moduleResolution": "node", "rootDir": "./" }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bschveitzer picture bschveitzer  路  5Comments

fareshan picture fareshan  路  3Comments

lverledens picture lverledens  路  4Comments

JustGreg picture JustGreg  路  4Comments

josecolella picture josecolella  路  4Comments