Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnhandledPromiseRejectionWarning: TypeError: Class constructor PuzzleEntity cannot be invoked without 'new' #9692

Open
1 of 18 tasks
davidjmstewart opened this issue Jan 8, 2023 · 0 comments

Comments

@davidjmstewart
Copy link

davidjmstewart commented Jan 8, 2023

Issue description

Program throws an error when attempting to execute transactions

Expected Behavior

Database is queried without any issue

Actual Behavior

TypeError is thrown:

(node:29724) UnhandledPromiseRejectionWarning: TypeError: Class constructor PuzzleEntity cannot be invoked without 'new'
    at SelectQueryBuilder.createFromAlias (C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\node_modules\typeorm\query-builder\QueryBuilder.js:424:37)
    at SelectQueryBuilder.from (C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\node_modules\typeorm\query-builder\SelectQueryBuilder.js:164:32)
    at C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\dist\app.js:73:10
    at Generator.next (<anonymous>)
    at C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\node_modules\tslib\tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter (C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\node_modules\tslib\tslib.js:113:16)  
    at getCurrentPuzzle (C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\dist\app.js:68:40)
    at Object.<anonymous> (C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\dist\app.js:81:30)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:29724) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:29724) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Steps to reproduce

Code to reproduce:

import { BaseEntity, Column, ConnectionOptions, DataSource, Entity, PrimaryGeneratedColumn } from 'typeorm';

interface IPuzzle {
  puzzle: string[][];
  puzzle_num: number;
  active_from: Date;
  active_to: Date;
}

@Entity()
class PuzzleEntity extends BaseEntity implements IPuzzle {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('jsonb')
  puzzle: string[][];

  @Column({ unique: true })
  puzzle_num: number;

  @Column({ type: 'timestamptz', nullable: false })
  active_from: Date;

  @Column({ type: 'timestamptz', nullable: false })
  active_to: Date;
}


export const dbConnection: ConnectionOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'root',
  password: 'admin',
  database: 'test',
  synchronize: true,
  logging: false,
  entities: [PuzzleEntity],
};

console.log('Creating data source');
const appDataSource = new DataSource(dbConnection);
console.log('Initializing data source');

appDataSource.initialize();

const getCurrentPuzzle = async (): Promise<PuzzleEntity> => {
  const now = new Date();

  const res = await appDataSource
      .createQueryBuilder()
      .select("Puzzle")
      .from(PuzzleEntity, "Puzzle")
      .where(":now > Puzzle.active_from AND :now < Puzzle.active_to ", { now })
      .orderBy('active_from', 'DESC')
      .getOne();
  return res;
}

console.log('Attempting to query');
getCurrentPuzzle().then(p => console.log(p));

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["es2017", "esnext.asynciterable"],
    "typeRoots": ["node_modules/@types"],
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "pretty": true,
    "sourceMap": true,
    "declaration": true,
    "outDir": "dist",
    "allowJs": false,
    "noEmit": false,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "importHelpers": true,
    "baseUrl": "src",
    "downlevelIteration": true,
    "paths": {
      "@/*": ["*"],
      "@controllers/*": ["controllers/*"],
      "@databases": ["databases"],
      "@dtos/*": ["dtos/*"],
      "@entities/*": ["entities/*"],
      "@exceptions/*": ["exceptions/*"],
      "@interfaces/*": ["interfaces/*"],
      "@middlewares/*": ["middlewares/*"],
      "@routes/*": ["routes/*"],
      "@services/*": ["services/*"],
      "@utils/*": ["utils/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.json", ".env"],
  "exclude": ["node_modules", "src/http", "src/logs", "src/tests"]
}

Output:

Creating data source
Initializing data source
Attempting to query
(node:9148) UnhandledPromiseRejectionWarning: TypeError: Class constructor PuzzleEntity cannot be invoked without 'new'
    at SelectQueryBuilder.createFromAlias (C:\Users\David Stewart\Downloads\o_my_word-main\o_my_word-main\web_app\server\node_modules\typeorm\query-builder\QueryBuilder.js:424:37)

My Environment

Dependency Version
Operating System Windows 11
Node.js version 14.6.0
Typescript version 4.7.4
TypeORM version 0.3.11

Additional Context

I am in the middle of porting code that was written using TypeORM v 0.2.34. I've done my best to extract the smallest amount of code to reproduce the error. I cannot see how this code is materially different from how it was (working) with v 0.2.34 - i've checked similar issues such as this one but the solution of changing the target to es6 did not work for me.

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant