TypeError: Class constructor Model cannot be invoked without 'new'
See original GitHub issueI’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:
- Created 6 years ago
- Comments:10 (6 by maintainers)
Top Related StackOverflow Question
Hey @Olian04, you need to set
targetat least toes6intsconfig.jsonThis is because es5 classes cannot inherit es6 classes, see here https://stackoverflow.com/questions/30689817/es6-call-class-constructor-without-new-keyword
No problem 😃 then I close this issue now