Skip to content
Advertisement

findOne() in TypeORM not working properly and giving error

I want to get the prospectousId from Dto, but I am getting the below error:,

Error

tenant.controller.ts

@Post('/promote-prospectus')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Promoting a prospectus to a tenant.' })
  @ApiResponse({ status: HttpStatus.OK, description: 'successful operation', })
  async promoteAProspectus(@Res() response: Response, @Body() Dto: promoteProspectousDto) {
    let result = await this.tenantsService.promoteAProspectus(Dto);
    return response.status(HttpStatus.CREATED).json({ message: 'Prospectous promoted as a tenant by the system.', data: result });
  }

tenant.service.ts

 public async promoteAProspectus(Dto: promoteProspectousDto): Promise<Tenants> {
    /**
     * Scopes to be implement while promoting a prospectous to a tenant.
     * 
     * @scope 1 promote the prospectous to a tenant and construct DB.
     * @scope 2 Configure all default roles for the newly created tenant.
     * @scope 3 Configure administrator user. & assign the administrator role.
     *
     */

    let prospectus = await this.tenantsRepository.findOne({ Dto.prospectousId });
    console.log('hyyyyyyyyyyyyyyyyyyyyyyyyyyy', prospectus);
    if (prospectus) {
      const { id } = prospectus;
      // let tenant: Tenants = await this.promoteAsTenant(prospectus);
      // await this.rolesService.onBoardTenantDefaultRoles(tenant.tenantDb);
      // let administrator: Users = await this.onBoardAdministratorUser(RequestedBy);
      // await this.allocateAdministratorRole(administrator, tenant);
      return tenant;
    }

    throw new ConflictException(`Unable to promote.`);
  }

tenant.entity.ts

import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';

export enum ETenantStatus {
    TRIAL = 'TRIAL',
    ACTIVE = 'ACTIVE',
    BLOCKED = 'BLOCKED',
    ARCHIVED = 'ARCHIVED',
}

@Entity({ name: `${Tenants.name}` })
export class Tenants {

    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({ nullable: false })
    organization: string;

    @Column({ unique: false })
    db: string;

    @Column({ unique: true })
    key: string;

    @Column({ unique: true })
    domain: string;

    @Column({ nullable: false })
    location: string;

    @Column({ nullable: false })
    referedBy: string;

    @Column()
    @CreateDateColumn({ type: 'timestamp without time zone' })
    public createdAt: Date;

    @Column()
    @UpdateDateColumn({ type: 'timestamp without time zone' })
    public updatedAt: Date;
}

dto:

   import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsBoolean, IsNotEmpty } from "class-validator";

export class tenantsDto {
    @ApiProperty({ required: true, default: '' })
    @IsNotEmpty()
    organization: string;

    @ApiProperty({ required: true, default: '' })
    @IsNotEmpty()
    domain: string;

    @ApiPropertyOptional({ default: '' })
    notes: string;

    @ApiPropertyOptional({ default: '' })
    db: string;

    @ApiPropertyOptional({ default: '' })
    @IsNotEmpty()
    key: string;

    @ApiPropertyOptional({ default: '' })
    location: string;


    @ApiPropertyOptional({ default: '' })
    referedBy: string;

    @ApiPropertyOptional({ default: false })
    skipDbMigration: boolean;

    @ApiProperty({ default: true })
    @IsBoolean()
    acceptedTermsAndConditions: boolean;
}

export class promoteProspectousDto {
    @ApiProperty({ required: true, default: '' })
    @IsNotEmpty()
    prospectousId: string;

    @ApiProperty({ required: true, default: '' })
    @IsNotEmpty()
    key: string;
}

What to do to achieve this? Thanks in advance!

Advertisement

Answer

Try using the below code. findOne() is to be replaced by findOneBy() as they say in their changelog.

let prospectus = await this.tenantsRepository.findOneBy({ id: Dto.prospectousId });

See the Changelog of TypeORM for more:

findOne() signature without parameters was dropped. If you need a single row from the db you can use a following syntax:

const [user] = await userRepository.find()

findOne(id) signature was dropped. Use following syntax instead:

const user = await userRepository.findOneBy({
   id: id // where id is your column name
})
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement