Home > Net >  FindOne function not working with Postgres
FindOne function not working with Postgres

Time:10-23

company.service.ts:`

async get(id: FindOneOptions<Company>): Promise<Company | null>{    
console.log(id)
return await this.companyRespository.findOne(id);
}

company.controller.ts:

@Get(':id')
  findOneCompany(@Param('id') id: FindOneOptions<Company>){
    return this.companyService.get(id);
 }

I have written the following code but whenever I try to get by FindOne my console says that "You must provide selection conditions in order to find a single row.". I tried all possible solution on the web including adding curly braces and also using other FindOptions nothing seems to make this error go away. My API platform shows an internal server error.

CodePudding user response:

Try to specify the Entity field that you're searching by. In your case this is the id.

company.service.ts

...
async get(id: FindOneOptions<Company>): Promise<Company | null>{    
    console.log(id)
    return await this.companyRespository.findOne({ id });
}

CodePudding user response:

async get(id: FindOneOptions<Company>): Promise<Company | null>{    
console.log(id)
return await this.companyRespository.findOne({id: id});
}

CodePudding user response:

If it's an id I don't know why it's type is an interface. I think that's why you have vscode telling you that's an error. You should as @DinuMinhea says:

service.ts

async get(id: string | number): Promise<Company | null>{    
console.log(id)
return await this.companyRespository.findOne({where: {id}});
}

company.controller.ts:

@Get(':id') findOneCompany(@Param('id') id: string | number){ 
    return this.companyService.get(id); 
  }
  • Related