typeorm: order of entities breaks code

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[x] postgres

TypeORM version:

[x] 0.2.14 (or put your version here)

Steps to reproduce or a small repository showing the problem:

So I’m trying to get this very simple OneToMany example work:

video:

import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, CreateDateColumn, UpdateDateColumn, ManyToOne } from "typeorm";
import { User } from "./user"
@Entity()
export class Video extends BaseEntity {

  @PrimaryGeneratedColumn('uuid')
  id: string

  @Column()
  title: string

  @CreateDateColumn()
  createdAt: Date

  @UpdateDateColumn()
  updatedAt: Date

  @ManyToOne(type => User, user => user.videos)
  user: User
}

User.ts

import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, CreateDateColumn, UpdateDateColumn, OneToMany } from "typeorm";
import { Video } from "./video"

@Entity()
export class User extends BaseEntity {

  @PrimaryGeneratedColumn('uuid')
  id: string

  @Column()
  firstName: string

  @Column()
  lastName: string

  @Column()
  email: string

  @Column({default: false})
  isEmailVerified: boolean

  @Column({default: false})
  isPhoneVerified: boolean

  @OneToMany(type => Video, video => video.user)
  videos: Video[]

  @CreateDateColumn()
  createdAt: Date

  @UpdateDateColumn()
  updatedAt: Date
}

and in my super simple experss server:

import * as express from 'express';
import { Request, Response } from 'express';
import { User } from "./entity/user"
import { Video } from "./entity/video"
const app = express();
const {
  PORT = 3000,
} = process.env;

import "reflect-metadata";

import {createConnection} from "typeorm";

const p: string = "./entity/*.ts"

createConnection({
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "abc",
    password: "123456",
    database: "abc",
    entities: [User, Video],
    synchronize: true,
    logging: false
}).then(connection => {
    // here you can start to work with your entities
    console.log("connected to db")
}).catch(error => console.log(error));

app.get('/', async (req: Request, res: Response) => {

  console.log("trying typeorm")

  try {
    
    const user = new User();
    user.firstName = "Timber";
    user.lastName = "Saw";
    user.email = "abc@gmail.com"
    await user.save()
  } catch (err) {
    console.log(err)

  }

  res.send({
    message: 'hello world',
  });
});

app.listen(PORT, () => {
  console.log('http server started at http://localhost:' + PORT);
  console.log("haha")
});

This throws an error:

/Users/exie/workspace/shandianyun-http/dist/http-server.js:293
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "User", function() { return User; });
                                                                                               ^

ReferenceError: User is not defined
    at Module.User (/Users/exie/workspace/shandianyun-http/dist/http-server.js:293:96)
    at Module../src/http-server/entity/video.ts (/Users/exie/workspace/shandianyun-http/dist/http-server.js:400:65)
    at __webpack_require__ (/Users/exie/workspace/shandianyun-http/dist/http-server.js:20:30)
    at Module../src/http-server/entity/user.ts (/Users/exie/workspace/shandianyun-http/dist/http-server.js:296:64)
    at __webpack_require__ (/Users/exie/workspace/shandianyun-http/dist/http-server.js:20:30)
    at Module.<anonymous> (/Users/exie/workspace/shandianyun-http/dist/http-server.js:421:70)
    at Module../src/http-server/index.ts (/Users/exie/workspace/shandianyun-http/dist/http-server.js:478:30)
    at __webpack_require__ (/Users/exie/workspace/shandianyun-http/dist/http-server.js:20:30)
    at __dirname../node_modules/process/browser.js.module.exports (/Users/exie/workspace/shandianyun-http/dist/http-server.js:84:18)
    at Object.<anonymous> (/Users/exie/workspace/shandianyun-http/dist/http-server.js:87:10)
[nodemon] app crashed - waiting for file changes before starting...

However as long as I switch the order of import in the express server, it works fine

import { Video } from "./entity/video"
import { User } from "./entity/user"

I’m wondering what’s the reason? Is it circular importing in some way so it breaks my code?

Do I need to check the import order each time I add a new model?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

@ISNIT0 try this:

{
  "compilerOptions": {
    "jsx": "react",
    "module": "commonjs",  // https://stackoverflow.com/questions/48378495/recommended-typescript-config-for-node-8
    "moduleResolution": "node", // choose node instead of classic 
    "noImplicitAny": false,
    "sourceMap": true,
    "outDir": "dist",
    "target": "es2017",
    "emitDecoratorMetadata": true, // required by typeorm
    "experimentalDecorators": true // required by typeorm
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
      "node_modules"
  ]
}