search

Home  >  Q&A  >  body text

How to get multiple data with the same user ID from a table in Nest.js? Assuming I have a users table, how do I get the data that matches the user ID?

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粉116654495P粉116654495417 days ago808

reply all(2)I'll reply

  • P粉998100648

    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 .

    reply
    0
  • P粉423694341

    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.

    reply
    0
  • Cancelreply