Sequelize Typescript Sequelize Typescript TypeError:没有'new'就不能调用类构造函数模型

Posted

技术标签:

【中文标题】Sequelize Typescript Sequelize Typescript TypeError:没有\'new\'就不能调用类构造函数模型【英文标题】:Sequelize Typescript Sequelize Typescript TypeError: Class constructor Model cannot be invoked without 'new'Sequelize Typescript Sequelize Typescript TypeError:没有'new'就不能调用类构造函数模型 【发布时间】:2018-10-20 16:41:13 【问题描述】:

我使用sequelize-typescript 运行示例mocha 测试来检查它我得到:

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

Repo to reproduce here(具体步骤见下文)。

我在网上看到了一些相关的问题,但我正在使用最新的从 typescript 转换为 javascript 的转换器,请参阅:

backend/tsconfig.json:

"target": "es2017",
    "lib": [
      "es2017",

相关代码为:

backend/src/test/ts/transaction.async.mocha.test.ts:

@Table
export class User extends Model<User> 

  @Column(primaryKey: true)
  id: string;

  @Column
  name: string;


async function asyncSaveAndFindTest(): Promise<User[]> 
  return await new User( name: 'someName' ).save().then(res => User.findAll());

这是重现问题的方法:

➜  tmp git clone https://github.com/avidanyum/test-node-sequelize
Cloning into 'test-node-sequelize'...
remote: Counting objects: 23, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 23 (delta 2), reused 23 (delta 2), pack-reused 0
Unpacking objects: 100% (23/23), done.
➜  tmp cd test-node-sequelize
➜  test-node-sequelize git:(master) npm install

> sqlite3@4.0.0 install /Users/myuser/tmp/test-node-sequelize/node_modules/sqlite3
> node-pre-gyp install --fallback-to-build

[sqlite3] Success: "/Users/myuser/tmp/test-node-sequelize/node_modules/sqlite3/lib/binding/node-v57-darwin-x64/node_sqlite3.node" is installed via remote
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN codelyzer@4.2.1 requires a peer of @angular/compiler@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/core@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/platform-browser-dynamic@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/platform-browser@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@4.2.1 requires a peer of @angular/common@>=2.3.1 <7.0.0 || >6.0.0-beta <7.0.0 but none is installed. You must install peer dependencies yourself.

added 649 packages in 21.194s
➜  test-node-sequelize git:(master) npm run-script test-mocha

> test-nodejs-sequelize@0.0.0 test-mocha /Users/myuser/tmp/test-node-sequelize
> mocha --require ts-node/register ./backend/src/test/ts/**/*.mocha.test.ts

sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules/sequelize/lib/sequelize.js:242:13


  DB Model Test
Executing (default): SELECT 1+1 AS result
Executing (default): CREATE TABLE IF NOT EXISTS `User` (`id` VARCHAR(255) PRIMARY KEY, `name` VARCHAR(255));
Executing (default): PRAGMA INDEX_LIST(`User`)
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_User_1`)
Executing (default): CREATE TABLE IF NOT EXISTS `User` (`id` VARCHAR(255) PRIMARY KEY, `name` VARCHAR(255));
Executing (default): PRAGMA INDEX_LIST(`User`)
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_User_1`)
Executing (2d1b9e42-32c2-4103-8f1d-17ad02279bd3): BEGIN DEFERRED TRANSACTION;
before add user 1
Executing (2d1b9e42-32c2-4103-8f1d-17ad02279bd3): ROLLBACK;
    1) should automatically pass transaction


  0 passing (36ms)
  1 failing

  1) DB Model Test
       should automatically pass transaction:
     TypeError: Class constructor Model cannot be invoked without 'new'
      at new User (backend/src/test/ts/transaction.async.mocha.test.ts:73:42)
      at /Users/myuser/tmp/test-node-sequelize/backend/src/test/ts/transaction.async.mocha.test.ts:50:20
      at step (backend/src/test/ts/transaction.async.mocha.test.ts:51:23)
      at Object.next (backend/src/test/ts/transaction.async.mocha.test.ts:32:53)
      at /Users/myuser/tmp/test-node-sequelize/backend/src/test/ts/transaction.async.mocha.test.ts:26:71
      at Promise (<anonymous>)
      at __awaiter (backend/src/test/ts/transaction.async.mocha.test.ts:22:12)
      at asyncSaveAndFindTest (backend/src/test/ts/transaction.async.mocha.test.ts:98:20)
      at Object.<anonymous> (backend/src/test/ts/transaction.async.mocha.test.ts:39:13)
      at step (backend/src/test/ts/transaction.async.mocha.test.ts:51:23)
      at Object.next (backend/src/test/ts/transaction.async.mocha.test.ts:32:53)
      at /Users/myuser/tmp/test-node-sequelize/backend/src/test/ts/transaction.async.mocha.test.ts:26:71
      at Promise (<anonymous>)
      at __awaiter (backend/src/test/ts/transaction.async.mocha.test.ts:22:12)
      at /Users/myuser/tmp/test-node-sequelize/backend/src/test/ts/transaction.async.mocha.test.ts:37:37
      at transaction.prepareEnvironment.then (node_modules/sequelize/lib/sequelize.js:998:21)
      at clsBind (node_modules/cls-hooked/context.js:172:17)
      at tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (node_modules/bluebird/js/release/promise.js:512:31)
      at Promise._settlePromise (node_modules/bluebird/js/release/promise.js:569:18)
      at Promise._settlePromise0 (node_modules/bluebird/js/release/promise.js:614:10)
      at Promise._settlePromises (node_modules/bluebird/js/release/promise.js:693:18)
      at Async._drainQueue (node_modules/bluebird/js/release/async.js:133:16)
      at Async._drainQueues (node_modules/bluebird/js/release/async.js:143:10)
      at Immediate.Async.drainQueues [as _onImmediate] (node_modules/bluebird/js/release/async.js:17:14)



npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test-nodejs-sequelize@0.0.0 test-mocha: `mocha --require ts-node/register ./backend/src/test/ts/**/*.mocha.test.ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test-nodejs-sequelize@0.0.0 test-mocha script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/myuser/.npm/_logs/2018-05-10T11_45_46_773Z-debug.log

【问题讨论】:

【参考方案1】:

在 tsconfig.json 中将目标更改为ES2017 解决了!

【讨论】:

以上是关于Sequelize Typescript Sequelize Typescript TypeError:没有'new'就不能调用类构造函数模型的主要内容,如果未能解决你的问题,请参考以下文章

了解 Sequelize 中的关联

Sequelize 从 belongsTo 关系中获取值

Sequelize包括范围为1的对象:m constraint = false

使用自定义连接表主键 Sequelize BelongsToMany

sequelize/sequelize-typescript - 带有 HasMany 的 findAll 返回一个对象而不是数组

Sequelize Typescript Sequelize Typescript TypeError:没有'new'就不能调用类构造函数模型