Dam*_*mon 11 typescript typeorm nestjs
我知道有很多关于这个主题的帖子。我真的很难理解我到底想做什么来解决这个问题。使用 Postman,当我尝试命中路线时,出现以下错误:
ERROR [ExceptionsHandler] No metadata for "OrganizationsRepository" was found.
EntityMetadataNotFoundError: No metadata for "OrganizationsRepository" was found.
Run Code Online (Sandbox Code Playgroud)
这是我的代码的样子
// app.module.ts
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
database: 'my-database',
username: 'postgres',
password: 'password',
autoLoadEntities: true,
synchronize: true,
}),
ConfigModule.forRoot({
isGlobal: true,
}),
OrganizationsModule,
],
controllers: [],
providers: [],
exports: [],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
// organizations.repository.ts
@EntityRepository(Organization). // this is showing as deprecated
export class OrganizationsRepository extends Repository<Organization> {
...
}
Run Code Online (Sandbox Code Playgroud)
// organization.entity.ts
@Entity({ name: 'organizations' })
export class Organization extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
name: string;
...
Run Code Online (Sandbox Code Playgroud)
我相信我需要创建一个新的DataSource
——或更具体地说,一个自定义存储库?
我一直使用上面的方法从我的数据库中读取数据,没有任何问题。现在突然间我收到此错误,并且我不确定如何在我的代码中解决。
EntityRepository 已被弃用,因此您可以用 Injectable() 替换它,然后您需要修改您的存储库、模块和服务。我给你举了例子,希望对你有帮助。
存储库.ts
import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Organization } from './Organization.entity';
@Injectable() // here
export class OrganizationRepository extends Repository<Organization> {
constructor(private dataSource: DataSource) {
super(Organization, dataSource.createEntityManager());
}
sync createOrganization({ title, description }: CreateOrganizationDto):Promise<Organization> {...}
}
Run Code Online (Sandbox Code Playgroud)
组织.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Organization } from './Organization.entity';
import { OrganizationController } from './Organization.controller';
import { OrganizationRepository } from './Organization.repository';
import { OrganizationService } from './Organization.service';
@Module({
imports: [TypeOrmModule.forFeature([Organization])],
controllers: [OrganizationController],
providers: [OrganizationService, OrganizationRepository],
})
export class OrganizationModule {}
Run Code Online (Sandbox Code Playgroud)
组织.service.ts
import { Injectable, NotFoundException } from '@nestjs/common';
import { OrganizationRepository } from './organization.repository';
import { InjectRepository } from '@nestjs/typeorm';
import { Organization } from './organization.entity';
@Injectable()
export class OrganizationService {
constructor(private readonly OrganizationRepository: OrganizationRepository) {}
async getOrganizationById(id: string): Promise<Organization> {
const record = this.OrganizationRepository.findOne({ where: { id } });
if (!record) {
throw new NotFoundException();
}
return record;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10184 次 |
最近记录: |