Nestjs with Typeorm for Mysql - Nodejs

Asked

Viewed 860 times

0

I’m starting in the area so sorry if it’s something silly.

I started developing a server using Nestjs with Typeorm to connect to a Mysql database but am picking up the following error: 'No Repository for "User" was found. Looks like this Entity is not Registered in Current "default" Connection?'

Without using Typeorm I can run the server.

"User" is my entity.

app module.

@Module({
    imports: [
        TypeOrmModule.forRoot({
            type: 'mysql',
            host: 'localhost',
            port: 3306,
            username: 'root',
            password: '',
            database: 'meuserver',
            entities: [`${__dirname}/**/*.entity{.ts,.js}`],
            synchronize: true,
            logging: true
        }),
        UserModule
    ],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule {
    constructor(
        private connection: Connection,
    ) { }
}

user module.

@Module({
    imports: [
        TypeOrmModule.forFeature([
            User,
        ]),
    ],
    providers: [
        UserService,
    ],
    controllers: [
        UserController,
    ]
})
export class UserModule { }

user Entity.

@Entity('user')
export class User extends BaseEntity {

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

    @Column({ default: false })
    active?: boolean;
}

Could you tell me what I’m doing wrong?

Thank you in advance!

2 answers

1

Welcome to our community, Littlefish!

The decorator @Column shall receive an object of the type Columnoptions (Decorators Reference).

Therefore, in the lastname attribute, the decorator should look like this:

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

Otherwise it will be interpreted as a 'data type', not as 'column name'. ;)

  • I adjusted it, but the error persists

  • @Littlefish delete the 'dist' folder and run it again.

  • The problem was in my Typeormmodule configs. The path of entities was wrong. Now I’m picking up another error but it’s another topic. Thanks for the help

0


It worked when I passed the settings to a 'ormconfig.json' file Goes below:

app module.

@Module({
    imports: [
        TypeOrmModule.forRoot(),
        UserModule
    ],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule {
    constructor(
        private connection: Connection,
    ) { }
}

ormconfig.json

{
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "root",
    "password": "",
    "database": "meuserver",
    "synchronize": true,
    "logging": true,
    "autoLoadEntities": true,
    "entities": ["./src/**/*.entity{.ts,.js}", "./dist/**/*.entity.js"]
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.