I have a nestJS project with a monorepo structure and face difficulties with this and context.
I have an app file: app.service.ts and an inner lib generated via Nest CLI.
app.services.ts has the following code logic:
//import dependencies
@Injectable()
export class AppService implements OnApplicationBootstrap {
  private readonly logger = new Logger('SomeName');
  private readonly ENV_VARIABLE = config.from();
  private ws: WebSocket;
  constructor(
    @InjectRepository(RowEntity) //Repository from TypeORM
    private readonly postgresRepository: Repository<RowEntity>,
    private readonly otherService: LibService, // import from @app/lib
  ) {}
  async onApplicationBootstrap(): Promise<void> {
    await this.loadInitial();
  }
  async loadInitial() {
    this.ws = new WebSocket(url); // standart web socket connection
    const connection = new this.ws() // connection works fine
    addListener(connection, this.logger, this.ProblemSave);  //such as Listener
    /**
     * BUT! 
     * await this.LibService.getMethod(input.toLowerCase()); 
     * works well here!
     */
  }
  async ProblemSave(input: string) {
    /**
     * PROBLEM HERE!
     * NestJS losing context of this keyword when executing via Listener
     */
    const data = await this.LibService.getMethod(input.toLowerCase()); // drops with error, since this undefined
    console.log(data);
    await this.postgresRepository.save(data);
  }
So my problem is shown above. I have a functional method inside class service, created in Nest, which is called as a function inside another method. But somehow in one case, this inside class methods works fine. But if I pass it in another method, the context of this lost and my function fails, with this.LibService is undefined error.
What should I do to solve the problem?
The listener code is below if someone is interested.
export function addListener(
  connection: connectionInterface,
  logger: Logger,
  saveFunc: FunctionInterface,
): void {
  connection.events({}, async (error: ErrnoException, {
    returnValues,
  }: {
    returnValues: ObjectInterface
  }) => {
    if (error) {
      logger.log(error);
      return;
    }
    try {
      //Execution works fine, but fails, because saveFunction doesn't have this context
      await saveFunc({
        input
      });
      logger.log(`Event created with id ${id}`);
      return;
    } catch (e) {
      console.error('ERROR', e);
      logger.log(e);
    }
  })
    .on('connected', (subscriptionId: string) => {
      logger.log(`subscribed to events with id ${subscriptionId}`);
    })
    .on('error', (error: ErrnoException) => {
      logger.log('error');
      logger.log(error);
    });
}
                there are couple of ways, first solution is to bind yow ProblemSave method in the constructor
export class AppService {
  constructor () {
    this.ProblemSave = this.ProblemSave.bind(this);
  }
   ProblemSave () {
     //stuff
   }
}
another solution is to use them arrow functions instead of methods
export class AppService {
  constructor () {}
   ProblemSave  = () => {
     //stuff
   }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With