![](/img/trans.png)
[英]TYPEORM RepositoryNotFoundError: No repository for "i" was found. Looks like this entity is not registered in current "default" connection?
[英]Nestjs Typeorm postgresql Error No repository '***' was found. Looks like this entity is not registered in current "default" connection?
使用nestjs cli(nest app)從單體應用轉換為單體存儲庫時會觸發此問題。 令人驚訝的是,它會像這樣返回存儲庫的錯誤。
文件:
auth/auth.module.ts
Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT),
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_DB,
autoLoadEntities: true,
synchronize: true,
}),
UsersModule,
],
controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}
身份驗證/用戶/users.module.ts
@Module({
imports: [
TypeOrmModule.forFeature([
UsersRepository,
UserDeviceRepository,
UserLocationRepository,
UserOTPRepository,
UserCredentialRepository,
]),
],
providers: [UserService, JwtStrategy, OtpService],
controllers: [UserController],
exports: [JwtStrategy, PassportModule],
})
export class UsersModule {}
身份驗證/用戶/存儲庫/user.repository.ts
@EntityRepository(Users)
export class UsersRepository extends Repository<Users> {
async createCustomer(
signUpCredentialsDto: SignUpCredentialsDto,
): Promise<Users> {
const { phone } = signUpCredentialsDto;
const customer = this.create({
phone: phone,
is_phone_verified: false,
is_email_verified: false,
is_complete_profile: false,
is_suspended: false,
access_level: UserAccessLevel.CUSTOMER,
});
try {
await this.save(customer);
return customer;
} catch (error) {
// duplicate user (phone,email)
if (error.code === '23505') {
throw new ConflictException('Phone / email already exists');
} else {
console.error(error);
throw new InternalServerErrorException();
}
}
}
}
身份驗證/用戶/實體/user.entity.ts
@Entity()
export class Users {
@PrimaryGeneratedColumn('increment')
id_user: number;
@Column({ nullable: true })
name: string;
@Column('varchar', { length: 15, unique: true })
phone: string;
@Column({ unique: true, nullable: true })
email: string;
@Column({ type: 'date', nullable: true })
birth_date: string;
@Column({ nullable: true })
occupation: string;
@Column()
is_phone_verified: boolean;
@Column()
is_email_verified: boolean;
@Column()
is_complete_profile: boolean;
@Column()
is_suspended: boolean;
@Column()
access_level: string;
@Column({ nullable: true })
gender: string;
@OneToMany((_type) => UserDevice, (device) => device.user, { eager: false })
device: UserDevice[];
@OneToMany((_type) => UserLocation, (location) => location.user, {
eager: false,
})
location: UserLocation[];
@OneToOne((_type) => UserCredentials, (credential) => credential.user)
@Exclude({ toPlainOnly: true })
credential: UserCredentials;
}
令人驚訝的是,在單體應用中它仍然可以正常運行。 我已經對此進行了 2 天的審查,並且沒有運氣查看互聯網。 非常感謝我能得到的任何幫助。 謝謝!
嘗試像這樣在 Typeorm 模塊中為實體提供路徑
TypeormModule.forRoot({
entities: [join(__dirname, '**', '*.entities.{js,ts}')],
/* rest of config */
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.