How to get multiple data with the same user ID from a table in Nestjs? Let's say I have a users table. How to get user ID matching data?
import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { usertbl } from './usertbl.entity'; @Injectable() export class UsersService { constructor( @InjectRepository(usertbl) private UsertblRepository: Repository<usertbl>, ) {} findAll(): Promise<usertbl[]> { return this.UsertblRepository.find(); } findOne(User_ID: string): Promise<usertbl> { return this.UsertblRepository.findOneBy({ User_ID }); } createusertbl(Usertbl: usertbl ): Promise<usertbl> { return this.UsertblRepository.save(Usertbl); } }
P粉9981006482023-11-17 19:29:36
If you want multiple matches, you should use the findBy method instead of findOne.
const Usertbl = await this.usersService.findBy({ id: 111 });
You can find more information on the type orm documentation .
P粉4236943412023-11-17 10:49:31
cosnt usertbl = await this.usersService.find({where: {User_ID: User_ID }})
This should work, but I recommend checking the typeorm documentation or wanago.
I would also recommend changing the variable names and try to follow camel case and uppercase type.