question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Class constructor Model cannot be invoked without 'new'

See original GitHub issue

I’m trying to use sequelize-typescript like this:

import { Model, Table, Column, Sequelize } from 'sequelize-typescript';

@Table
class User extends Model<User> {
  @Column name: string;
  @Column age: number;
}

const seq = new Sequelize({
  database: '../database.db',
  username: 'root',
  password: '',
  dialect: 'sqlite',
  storage: '../database.db'
});
seq.addModels([User]);

const user = new User({ 
  name: 'oliver twist',
  age: 14
});
user.save();

But its throwing a really weird error. It’s saying that Class constructor Model cannot be invoked without 'new' and then it point at some random part of the code, that has nothing to do with sequelize-typescript, and its a different part each time i make a change to the code (no matter how small of a change). But the error goes away if I remove the call to new User. Why is this? Also, i can’t init User with User.build or User.create as the README would suggest, there simply isn’t a build or create method on the User object…

Versions:

  • sequelize-typescript: 0.6.2
  • sequelize: 4.34.0
  • sqlite3: 3.1.13
  • node: 9.4.0
  • typescript: 2.7.2
  • ts-node: 5.0.0
  • reflect-metadata: 0.1.12

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "lib": [ "ES2015" ],                             /* Specify library files to be included in the compilation. */
    "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    "emitDecoratorMetadata": true          /* Enables experimental support for emitting type metadata for decorators. */
  }
}

Update1:
After forcefully trying to use create, it turns out that it was a problem with the vscode instellisense, and the create method does exist.

User.create({
  name: 'oliver twist',
  age: 14
});

However the error remains…

Update2:
After some more searching i found issue https://github.com/RobinBuschmann/sequelize-typescript/issues/26 which solved this problem. However now I’m getting Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: User

Update3:
Turns out I had to put the create call inside of an anonymous async function call.

(async () => {
  User.create({
    name: 'oliver twist',
    age: 14
  });
  const allUsers = await User.findAll();
  console.log(allUsers.map(u => u.name));
})();

In case this is intended, this really needs to be communicated better in the README… If it’s not intended, then how am i supporsed to use it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
RobinBuschmanncommented, Feb 26, 2018

Hey @Olian04, you need to set target at least to es6 in tsconfig.json

This is because es5 classes cannot inherit es6 classes, see here https://stackoverflow.com/questions/30689817/es6-call-class-constructor-without-new-keyword

0reactions
RobinBuschmanncommented, Mar 3, 2018

No problem 😃 then I close this issue now

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Class constructor model cannot be invoked without ...
When I run my app, an error appears: TypeError: Class constructor model cannot be invoked without 'new' at line 17 which is the...
Read more >
Class constructor Model cannot be invoked without 'new' and ...
Hi all, I using v4 to defined my model as: @Options({ sequelize, tableName: 'V2_category', timestamps: false }) @Attributes({ id: { type: ...
Read more >
Javascript ES6 TypeError Class constructor Client cannot be ...
When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invoked without 'new'.
Read more >
Evitar el error de Class constructor model cannot be invoked ...
Solucionar el error: class constructor model cannot be invoked without 'new' en un proyecto Typescript con Sequelize.
Read more >
Class constructor NodeEnvironment cannot be invoked ...
To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. (Use node --trace ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found