服务 class 在节点中未定义

Service class is undefined in node

我想知道我做错了什么。当我尝试做

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

我收到错误消息

TypeError: Cannot read property 'findByAccountId' of undefined

这是我的代码

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;
        })
    }
}

你需要搬家 private readonly userService: UserService; 到构造函数,因此它将被依赖注入。您的代码应为:

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;
        })
    }
}

this.userService 未定义。您需要将其添加为构造函数的依赖项:

      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;
      }

如果 userService 是模块的一部分,则将其作为依赖项注入,否则应将其设置为模块引用;这里的例子:

Dependency injection:

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;
        })
    }
}

Module reference

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;
        })
    }
}