Search code examples
nestjs

Getting undefined error for the id of user


I have the user entity and the wallet entity, i have made the relation between them, which is in user entity:

   @OneToMany(() => Wallet, wallet => wallet.user)
    wallets: Wallet[];

and in wallet entity:

    @ManyToOne(() => User, user => user.wallets)
    user: User;

So, i want to activate the wallet by the user.id, and here is the function in the wallet.service:

  async walletActivation(walletId: number): Promise<Boolean> {
    const wallet = await this.walletsRepo.findOneBy({ id: walletId })
    if (!wallet) throw new NotFoundException('Wallet Not Found')
    // console.log(wallet);

    wallet.isActive = 0; // Ensure wallet is not active
    
    console.log("------");
    // console.log("------" + wallet.user.id);
    const user = await this.usersService.findOneById(wallet.user.id); // here is the problem
    if (!user) throw new NotFoundException('User associated with the wallet not found.')
    if (!user.isVerified) throw new BadRequestException('User account is not verified.')

    wallet.isActive = 1;

    await this.walletsRepo.save(wallet);
    return true;
  }

and here is the endpoint in the wallet.controller:

  @Post('activateWallet/:id')
  async walletActivation(@Param('id') id: string) {
    try {
      await this.walletsService.walletActivation(+id);
      return { message: 'Wallet activated successfully' };
    } catch (error) {
      // Log the error for debugging
      console.error('Error activating wallet:', error);

      // Check the type of error and return a more specific message
      if (error instanceof NotFoundException) {
        throw new HttpException('Wallet or user not found', HttpStatus.NOT_FOUND);
      } else if (error instanceof BadRequestException) {
        throw new HttpException('User is not verified', HttpStatus.BAD_REQUEST);
      } else {
        throw new HttpException('Wallet cannot be activated', HttpStatus.BAD_REQUEST);
      }
    }
  }

and here is the error i keep getting:

Error activating wallet: TypeError: Cannot read properties of undefined (reading 'id')
    at WalletsService.walletActivation (G:\abood\IT\Courses\Projects\NestJS\Wallet System\wallet-system\src\wallets\wallets.service.ts:33:66)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async WalletsController.walletActivation (G:\abood\IT\Courses\Projects\NestJS\Wallet System\wallet-system\src\wallets\wallets.controller.ts:32:7)

I have tried too many things, any suggestions??


Solution

  • If you are sure that your user entity has the id specified, the user is not loading, you should explicitly load the user relation:

    const wallet = await this.walletsRepo.findOne({
      where: { id: walletId },
      relations: ['user'], // Add this
    });

    Also the user null check should be above, before calling wallet.user.id anyway, but the query on user isn't necessary animore.

    The code should be something like this:

    async walletActivation(walletId: number): Promise<Boolean> {
        const wallet = await this.walletsRepo.findOne({
            where: { id: walletId },
            relations: ['user'], // add this
        });
        
        if (!wallet) throw new NotFoundException('Wallet Not Found');
    
        //wallet.isActive = 0; // why? not needed
        //const user = await this.usersService.findOneById(wallet.user.id);  //no longer needed, user is preloaded/joined from the query above
    
        if (!wallet.user) throw new NotFoundException('User associated with the wallet not found.'); //since the activation is for the wallet, you can use BadRequestException here, probably is better.
        if (!wallet.user.isVerified) throw new BadRequestException('User account is not verified.');
    
        wallet.isActive = 1;
    
        await this.walletsRepo.save(wallet);
        return true; //maybe is better to return the wallet entity here, you make it easier for the future or for the backend, without exposing wallet "sensitive" data
    }

    Without seeing your entities, the code above should be all you need.