Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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,
    },
  });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading