admin管理员组

文章数量:1294541

pany.service.ts:`

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

pany.controller.ts:

@Get(':id')
  findOneCompany(@Param('id') id: FindOneOptions<Company>){
    return thispanyService.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.

pany.service.ts:`

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

pany.controller.ts:

@Get(':id')
  findOneCompany(@Param('id') id: FindOneOptions<Company>){
    return this.panyService.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.

Share asked Oct 21, 2022 at 11:53 ChickreesChickrees 411 silver badge3 bronze badges 1
  • 1 What is the code of the CompanyRepository.findOne method ? – TLd Commented Oct 21, 2022 at 12:13
Add a ment  | 

4 Answers 4

Reset to default 6

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.panyRespository.findOne({where: {id}});
}

pany.controller.ts:

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

You should use findByOne rather than findOne, since findOne expects parameters such as id

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

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

pany.service.ts

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

本文标签: javascriptFindOne function not working with PostgresStack Overflow