Home > Blockchain >  Service class is undefined in node
Service class is undefined in node

Time:05-04

I would like to know what I am doing wrong. When I try to do

var users = await this.userService.findByAccountId(id_bd_account);

I get the error

TypeError: Cannot read property 'findByAccountId' of undefined

Here's my code

import { Injectable } from '@nestjs/common';
import { HttpService  } from '@nestjs/axios';
import {SaitAuthDto} from "./dto/sait-auth.dto";
import { AccountService } from "../account/account.service";
import { UserService } from '../user/user.service';

@Injectable()
export class ServicesSaitService {
    private readonly _saitCredential: SaitAuthDto = null;
    private readonly _urlSait: string = "";
    private readonly userService: UserService;
    private saitUrl =  "sait";
    private cuitUrl = "clienteCuentaCorriente";

    constructor(private readonly http: HttpService, private readonly acountService: AccountService ) {
        this._saitCredential = new SaitAuthDto();
        this._saitCredential.usuario = process.env.SAIT_USER;
        this._saitCredential.password = process.env.SAIT_USER_PASSWORD;
        this._urlSait = process.env.SAIT_BASE_URL;
    }
    
    public async getCustomerAccountByCuil(code:string, token:string): Promise<any> {
        const url = new URL([this.saitUrl, this.cuitUrl].join('/'), this._urlSait);
        url.searchParams.append('token', token);
        url.searchParams.append('codigo',code);
        return this.http.get(url.href).toPromise().then(async (value)=>{
            var bd_account = await this.acountService.getAccountByCuit(value.data.registros[0]['cuit']);
            var id_bd_account = bd_account[0].id;
            if(bd_account.length !== 0 ){
                var users = await this.userService.findByAccountId(id_bd_account);
                console.log(users);
            }
            return value.data;
        }).catch((error)=>{
            console.log(error);
            throw error;
        })
    }
}

CodePudding user response:

You need to move private readonly userService: UserService; to the constructor so it will be dependency injected. Your code should read:

import { Injectable } from '@nestjs/common';
import { HttpService  } from '@nestjs/axios';
import {SaitAuthDto} from "./dto/sait-auth.dto";
import { AccountService } from "../account/account.service";
import { UserService } from '../user/user.service';

@Injectable()
export class ServicesSaitService {
    private readonly _saitCredential: SaitAuthDto = null;
    private readonly _urlSait: string = "";
    private saitUrl =  "sait";
    private cuitUrl = "clienteCuentaCorriente";

    constructor(private readonly http: HttpService, private readonly acountService: AccountService, private readonly userService: UserService ) {
        this._saitCredential = new SaitAuthDto();
        this._saitCredential.usuario = process.env.SAIT_USER;
        this._saitCredential.password = process.env.SAIT_USER_PASSWORD;
        this._urlSait = process.env.SAIT_BASE_URL;
    }
    
    public async getCustomerAccountByCuil(code:string, token:string): Promise<any> {
        const url = new URL([this.saitUrl, this.cuitUrl].join('/'), this._urlSait);
        url.searchParams.append('token', token);
        url.searchParams.append('codigo',code);
        return this.http.get(url.href).toPromise().then(async (value)=>{
            var bd_account = await this.acountService.getAccountByCuit(value.data.registros[0]['cuit']);
            var id_bd_account = bd_account[0].id;
            if(bd_account.length !== 0 ){
                var users = await this.userService.findByAccountId(id_bd_account);
                console.log(users);
            }
            return value.data;
        }).catch((error)=>{
            console.log(error);
            throw error;
        })
    }
}

CodePudding user response:

this.userService isn't defined. You need to add it as a dependency to your constructor:

      constructor(private readonly http: HttpService, private readonly acountService: AccountService, private userService: UserService) {
           this._saitCredential = new SaitAuthDto();
           this._saitCredential.usuario = process.env.SAIT_USER;
           this._saitCredential.password = process.env.SAIT_USER_PASSWORD;
           this._urlSait = process.env.SAIT_BASE_URL;
      }
  • Related