How do I get multiple data with same user id from a table in nestjs? suppose I have a user table. How can I get user id matched 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);
}
}
CodePudding user response:
cosnt usertbl = await this.usersService.find({where: {User_ID: User_ID }})
This should work but I would recommend checking the typeorm documentation or this article on wanago
I would recommend also changing the name of variables try following camel case and for types capitalized
CodePudding user response:
If you want to have multiple matches you should use the findBy method instead of the findOne.
const Usertbl = await this.usersService.findBy({ id: 111 });
You can find more information on the type orm documentation.