Issue
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;
})
}
}
Solution
inject userService
as dependency if it part of your module if not you should set it as module reference; here the examples:
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;
})
}
}
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';
import { ModuleRef } from '@nestjs/core';
@Injectable()
export class ServicesSaitService {
private readonly _saitCredential: SaitAuthDto = null;
private readonly _urlSait: string = "";
private saitUrl = "sait";
private cuitUrl = "clienteCuentaCorriente";
private readonly userService: UserService
constructor(private readonly http: HttpService, private readonly acountService: AccountService, private moduleRef: ModuleRef) {
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;
}
onModuleInit() {
this.userService = this.moduleRef.get(UserService, {
strict: false,
});
}
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;
})
}
}
Answered By - Martinez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.