3

I am new to typescript + typeorm and got stuck into following issue for quite a long time now. I read lot of issues on github but couldn't find the problem.

My project structure

<projectname>
   - src
     - entity
         - patient.ts
     - migration
     - app.ts
     - server.ts

   - package.json
   - ormconfig.json
     ...

package.json

{
"name": "fir-server",
"version": "0.0.0",
"private": true,
"main": "app.js",
"scripts": {
    "build": "tsc",
    "dev": "ts-node ./src/server.ts",
    "start": "nodemon ./dist/src/server.js",
    "test": "mocha --recursive './test/*Test.js' --compilers js:babel-core/register"
},
"dependencies": {
    "babel-core": "^6.25.0",
    "babel-polyfill": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.26.0",
    "express": "^4.15.4",
    "mysql2": "^1.6.1",
    "reflect-metadata": "^0.1.10",
    "typeorm": "0.2.7",
    ...
},
"devDependencies": {
    "@types/node": "^8.0.29",
    "ts-node": "3.3.0",
    "typescript": "2.5.2",
    ....
}
}

ormconfig.json

{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "db_user",
"password": "db_password",
"database": "firdb",
"synchronize": false,
"migrationsRun": true,
"logging": false,
"entities": [
    "dist/src/entities/*.js" 
    // tried this as well:  
    // src/entities/*.js
    // src/entities/**/**.js, src/entities/*.ts, src/entities/**/**.ts
],
"migrations": [
    "dist/src/migration/*.js"
],
"subscribers": [
    "dist/src/subscriber/*.js"
],
"cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
}

}

src/app.ts

import "reflect-metadata";
import * as express from "express";
import * as bodyParser from "body-parser";

import { Routes } from "../routes/index";

class App {
  public app: express.Application;
  public route: Routes = new Routes();

  constructor() {
    this.app = express();
    this.config();
    this.route.routes(this.app);
  }

  private config(): void {
    // support application/json type post data
    this.app.use(bodyParser.json());
    //support application/x-www-form-urlencoded post data
    this.app.use(bodyParser.urlencoded({ extended: false }));
  }

}

export default new App().app;

src/server.ts

import app from "./app"
import { createConnection } from 'typeorm';

const port = 3000

createConnection().then(async (connection) => {
   app.listen(port, () => {
      console.log('Express server listening on port ' + port);
   })
}).catch((error) => console.log(error));    

patient.ts

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class Patient {

@PrimaryGeneratedColumn()
id: number;

@Column({ name: "first_name" })
firstName: string;

@Column({ name: "middle_name" })
middleName: string;

@Column({ name: "last_name" })
lastName: string;

@Column()
username: string;

@Column()
gender: string;

@Column()
phone_number: string;

@Column()
email: string;

@Column()
is_mother: boolean;
}

After I run the command:

tsc

A new dist directory is created on project root with following structure:

- dist
    - src
        - entity
            - patient.js
            - patient.js.map

        - migration

I use it like this in my controller:

import { Patient } from 'src/entity/patient'

let patients = getConnection().manager.find(Patient);

The error I get:

UnhandledPromiseRejectionWarning: EntityMetadataNotFound: No metadata for "Patient" was found

2
  • 4
    In your config you have entities "dist/src/entities/*.js" but it seams you have your file in the entity folder... either rename the folder or change the config to look for the "dist/src/entity/*.js" Commented Oct 7, 2018 at 15:06
  • So stupid of me, thanks a lot @nemesv, you saved me lot of time, I don't know how I missed it :( Commented Oct 7, 2018 at 16:38

1 Answer 1

0

I recently had the same issues. While searching the internet, I found out that someone had a similar issue before. Therefore, I will be contributing my solution, perhaps somebody finds it helpful.

The error code is caused because TypeORM could not locate the directory which contains the entity.

First, we must understand that our files are being served from the dist folder after the build. So, the path has to be relative to the dist folder.

In my case, the datasource.ts was in src folder. It means after the build, that is when ts has been compiled, it will be in the root dist folder.

Solution that I used:


const AppDataSource = new DataSource( {
    type: 'mysql',
    host: DATABASE_HOST,
    port: DATABASE_PORT,
    username: DATABASE_USERNAME,
    password: DATABASE_PASSWORD,
    database: DATABASE_NAME,
    entities: [ `${ __dirname }/entity/*.entity.js`]
} );

NOTE: I named all my entity files ending in .entity.js. If you had used just .js, it will work fine.

As in:


  const AppDataSource = new DataSource( {
      type: 'mysql',
      host: DATABASE_HOST,
      port: DATABASE_PORT,
      username: DATABASE_USERNAME,
      password: DATABASE_PASSWORD,
      database: DATABASE_NAME,
      entities: [ `${ __dirname }/entity/*.js` ]
  } );

Check: [BUG] web/nestjs: EntityMetadataNotFoundError: No metadata was found. if you are still confused.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.