typescriptnestjsbackendtypeormnest

Object literal may only specify known properties, and 'id' does not exist in type 'FindOptionsWhere<Teacher>


I keep getting this error in my teachers.service:

Object literal may only specify known properties, and 'id' does not exist in type 'FindOptionsWhere | FindOptionsWhere[]'.ts(2353) FindOneOptions.d.ts(23, 5): The expected type comes from property 'where' which is declared here on type 'FindOneOptions' (property) id: number No quick fixes available

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, UpdateResult, DeleteResult } from 'typeorm';
import { Teacher } from '../entities/teacher.entity';

@Injectable()
export class TeachersService {
  constructor(
    @InjectRepository(Teacher)
    private teachersRepository: Repository<Teacher>,
  ) {}

  findAll(): Promise<Teacher[]> {
    return this.teachersRepository.find();
  }

  async findOne(id: number): Promise<Teacher | null> {
    return this.teachersRepository.findOne({
      where: {
        id,
      },
    });
  }

  create(teacher: Teacher): Promise<Teacher> {
    return this.teachersRepository.save(teacher);
  }

  async update(id: number, teacher: Teacher): Promise<Teacher> {
    await this.teachersRepository.update(id, teacher);
    return this.findOne(id);
  }

  async remove(id: number): Promise<void> {
    await this.teachersRepository.delete(id);
  }
}

My Teacher entity:

import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm'; 
import { Class } from './class.entity'; 

@Entity() 
export class Teacher { 
  @PrimaryGeneratedColumn() 
  teacher_id: number; 

  @Column() 
  teacher_name: string; 

  @Column() 
  teacher_lastname: string; 

  @Column() 
  teacher_email: string; 

  @OneToMany(() => Class, (classEntity) => classEntity.teacher) 
  classes: Class[]; 
} 

Solution

  • Field id does not exist on Teacher. In your where condition you can only specify fields that actually exist on your entity.

    async findOne(id: number): Promise<Teacher | null> {
      return this.teachersRepository.findOne({
        where: {
          id,
        },
      });
    }