based on this import { Column, Entity, OneToMany } from 'typeorm'; import {…
- User
based on this import { Column, Entity, OneToMany } from 'typeorm';
import { AbstractIdEntity } from '../../../common/abstract.id.entity'; import { EventEntity } from '../../event/entities/event.entity'; import { LocationDto } from '../dto/location.dto';
@Entity({ name: 'location' }) export class LocationEntity extends AbstractIdEntity { @Column({ nullable: true }) name: string;
@Column({ nullable: true, type: 'int' }) color: number; @OneToMany(() => EventEntity, (event) => event.location, { nullable: true }) events: EventEntity[]; dtoClass = LocationDto;} location entity why is this code if (createEventDto.locationId) { const location = await this.locationRepository.findOne( createEventDto.locationId, ); event.location = location; } throwing Type 'number' has no properties in common with type 'FindOneOptions'.ts(2559) (property) EventCreateDto.locationId: number
- Assistant
The code is throwing this error because
this.locationRepository.findOneexpects an object of typeFindOneOptions<LocationEntity>as parameter, butcreateEventDto.locationIdis a number. To fix the error, you can create aFindOneOptions<LocationEntity>object with thecreateEventDto.locationIdas the value of theidproperty, like this:if (createEventDto.locationId) { const location = await this.locationRepository.findOne({ id: createEventDto.locationId, }); event.location = location; } - User
now it throws Argument of type '{ id: number; }' is not assignable to parameter of type 'FindOneOptions'. Object literal may only specify known properties, and 'id' does not exist in type 'FindOneOptions'.ts(2345) (parameter) createEventDto: EventCreateDto
- Assistant
To fix this error, you can create a
FindOneOptions<LocationEntity>object with awhereproperty that specifies theidfield and its value, like this:if (createEventDto.locationId) { const location = await this.locationRepository.findOne({ where: { id: createEventDto.locationId }, }); event.location = location; }This will create a valid
FindOneOptions<LocationEntity>object with a where clause that selects theLocationEntitywith the specifiedidvalue.