tsedio / tsed

:triangular_ruler: Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone. ⭐️ Star to support our work!
https://tsed.io/
MIT License
2.84k stars 286 forks source link

[BUG] Injected repo undefined when built #919

Closed bpofficial closed 3 years ago

bpofficial commented 4 years ago

Information

After builing my project using tsc --project tsconfig.compile.json i get the below error when trying to create a new user. This doesn’t happen when running using npm start

The error occurs within my user service, as apparently the userRepository doesn't exist.

TypeError: Cannot read property 'save' of undefined
    at UserRepository.Repository.save (/Users/.../Server/dist/node_modules/typeorm/repository/Repository.js:78:29)
    at UserService.<anonymous> (/Users.../Server/dist/Services/Common/UserService.js:34:54)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/.../Server/dist/node_modules/tslib/tslib.js:110:62)

Configs:

// tsconfig.compile.json
{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "baseUrl": ".",
        "outDir": "./dist",
        "moduleResolution": "node",
        "declaration": true,
        "noResolve": false,
        "preserveConstEnums": true,
        "sourceMap": true,
        "noEmit": false,
        "inlineSources": true
    },
    "include": ["./src/**/*.ts"]
}
// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2016",
        "sourceMap": true,
        "declaration": false,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "moduleResolution": "node",
        "isolatedModules": false,
        "suppressImplicitAnyIndexErrors": false,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "allowSyntheticDefaultImports": true,
        "importHelpers": true,
        "newLine": "LF",
        "noEmit": true,
        "resolveJsonModule": true,
        "lib": ["es7", "dom", "esnext.asynciterable"],
        "typeRoots": ["./node_modules/@types"]
    },
    "include": ["./src/**/*.ts", "./tests/**/**.ts"],
    "exclude": ["node_modules", "./public", "dist"],
    "linterOptions": {
        "exclude": []
    }
}

My user repository is

import { EntityRepository, Repository } from 'typeorm';
import UserEntity from '../Entities/UserEntity';

@EntityRepository(UserEntity)
export default class UserRepository extends Repository<UserEntity> {
    // vibe
}

and my config file is

/* istanbul ignore file */
process.env.NODE_ENV !== 'production'
    ? require('dotenv').config({ path: '.env' })
    : () => {};
import * as Path from 'path';
import Controllers from '../Controllers/';
import Middleware from '../Middleware';
import Services from '../Services';
import cookies from './cookie.config';
import './extensions';
import typeorm from './typeorm.config';

export const rootDir = Path.resolve(__dirname, '../');
export default {
    rootDir,
    mount: {
        '/': Controllers,
    },
    componentsScan: [Services, Middleware],
    host: '127.0.0.1',
    port: process.env.SERVER_PORT,
    acceptMimes: ['application/json'],
    typeorm,
    cookies,
};

where typeorm config is

/* istanbul ignore file */
import Entities from '../Database/Entities';
const TESTING = process.env.NODE_ENV === 'test';
export const DATABASE_NAME = TESTING
    ? process.env.DB_TABLE + '_test'
    : process.env.DB_TABLE;

const SYNC = process.env.NODE_ENV !== 'production';

export default [
    {
        name: 'default',
        type: 'mysql',
        host: process.env.DB_HOST,
        username: process.env.DB_USER,
        password: process.env.DB_PASS,
        database: DATABASE_NAME,
        synchronize: SYNC,
        dropSchema: TESTING,
        entities: Entities,
    },
];

Any help or pointers would be awesome, I seem to keep running into trouble with TypeORM related issues :(

Romakita commented 4 years ago

Hello @bpofficial,

This issue means there is any connection associated to the repository. The userRepository exist but the connection is missing. I already seen this error but I don’t remember How i fixed that.

Romakita commented 4 years ago

@bpofficial Have you found the problem ?