Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

nestjs - ExceptionHandler No metadata for "Profile" was found in nest.js

I was creating Nest.js project and faced some problems with it. I assume the problem lies within profile.entity.ts file since if I remove that file then all works ok. So, I have several files app.module.ts file:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeOrmConfig } from './config/typeorm.config';
import { ProfileModule } from './profile/profile.module';

@Module({
  imports: [TypeOrmModule.forRoot(typeOrmConfig), ProfileModule],
  controllers: [],
  providers: [],
})
export class AppModule {}

profile.entity.ts

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

@Entity()
export class Profile extends BaseEntity{
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    fullName: string;
    @Column()
    company: string;
    @Column()
    position: string;
    @Column()
    dateOfBirth: Date;
    @Column()
    phoneNumber: string;
    @Column()
    additionalPhoneNumber: string;
    @Column()
    email: string;
    @Column()
    additionalEmail: string;
    @Column()
    website: string;
    @Column()
    additionalWebsite: string;
    @Column()
    facebook: string;
    @Column()
    instagram: string;
    @Column()
    telegram: string;
    @Column()
    whatsapp: string;
    @Column()
    vk: string;
    @Column()
    tikTok: string;
    @Column()
    linkedIn: string;
    @Column()
    youTube: string;
    @Column()
    skype: string;
    @Column()
    modeDetails: string;

}

profile.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProfileController } from './profile.controller';
import { ProfileRepository } from './profile.repository';
import { ProfileService } from './profile.service';

@Module({
  imports:[TypeOrmModule.forFeature([ProfileRepository])],
  controllers: [ProfileController],
  providers: [ProfileService]
})
export class ProfileModule {}

profile.repository.ts

import { Profile } from './profile.entity';
import { EntityRepository, Repository } from 'typeorm';

@EntityRepository(Profile)
export class ProfileRepository extends Repository<Profile> {}

typeorm.config.ts

import { TypeOrmModuleOptions } from "@nestjs/typeorm";

export const typeOrmConfig:TypeOrmModuleOptions={
    type:'postgres',
    host:'localhost',
    port:5432,
    username:'postgres',
    password:'M',
    database: 'profileManagement',
    entities:[__dirname + '/../**/*.entity.ts'],
    synchronize:true
}

Please help I cannot find the exact place where I did wrong

question from:https://stackoverflow.com/questions/65879765/exceptionhandler-no-metadata-for-profile-was-found-in-nest-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change your entities from __dirname + '/../**/*.entity.ts' to __dirname + '/../**/*.entity.js'. What's happening is that when you transpile the code from ts to js the code moves from src to dist. At this time, there is no ts code in the dist, so the glob you're using doesn't find anything. If you use .js however, it will find matching files, and be able to make use of the metadata that is set there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...