添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
满身肌肉的保温杯  ·  ssl medium strength ...·  7 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Using NestJS Axios, a simple http get request throws "Cannot read properties of undefined (reading 'emit')" error

Ask Question

Versions I'm using in my NestJS project (I show just the relevant packages):

  • "@nestjs/common": "^9.4.0",
  • "@nestjs/axios": "^2.0.0",
  • "axios": "^1.3.6",
  • "follow-redirects": "^1.15.2",
  • I've tried to follow the documentation here from the beginning: https://docs.nestjs.com/techniques/http-module

    I've created a module called geo.module.ts , which looks like this:

    import { Module } from '@nestjs/common';
    import { GeoService } from './geo.service';
    import { GeoController } from './geo.controller';
    import { HttpModule } from '@nestjs/axios';
    @Module({
      imports: [HttpModule.register({
        timeout: 5000,
        maxRedirects: 5,
      controllers: [GeoController],
      providers: [GeoService]
    export class GeoModule {}
    

    Created the controller which points to the service that's fine, and here is the service:

    import { HttpService } from '@nestjs/axios';
    import { Injectable } from '@nestjs/common';
    @Injectable()
    export class GeoService {
      constructor(private readonly httpService: HttpService) {}
      findSomething() {
        return this.httpService.get('http://urldoesntmatter.something');
    

    This function runs, and throws an error like this:

    [Nest] 424  - 2023. 04. 27. 7:49:48   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'emit')
    TypeError: Cannot read properties of undefined (reading 'emit')
        at Object.eventHandlers.<computed> [as abort] (C:\somewhere\node_modules\follow-redirects\index.js:14:24)
        at TransformOperationExecutor.transform (C:\somewhere\node_modules\src\TransformOperationExecutor.ts:207:39)      
        at TransformOperationExecutor.transform (C:\somewhere\node_modules\src\TransformOperationExecutor.ts:327:31)      
        at TransformOperationExecutor.transform (C:\somewhere\node_modules\src\TransformOperationExecutor.ts:327:31)      
        at TransformOperationExecutor.transform (C:\somewhere\node_modules\src\TransformOperationExecutor.ts:327:31)
    

    This error is true for any url I enter. I've googled it all day but can not resolve this issue why this happens, seems like based on the doc I didn't miss anything.

  • Maybe the versions aren't compactible with each other?
  • I've already tried to downgrade "follow-redirects" package also "axios" package, didn't work.
  • Already tried to use directly "axios" package, also tried to use axiosRef as well. Tried different urls but even on my locally running apis it throws this error.
  • Tried to modify the module to set "maxRedirects: 5" to 0. In this case there is a different error, and I also don't want to modify that value also it is 5 by default.
  • Tried to modify url to use https instead of http, no success. I've checked the file as well where the error happens but I still couldn't figure out the issue.
  • I've figured out.

    Based on THIS answer, I've just returning the data itself instead of the entire Axios object. This finally resolve the problem. (but this isn't in the doc. :) )

    So the updated code will look like this:

    import { HttpService } from '@nestjs/axios';
    import { Injectable } from '@nestjs/common';
    @Injectable()
    export class GeoService {
      constructor(private readonly httpService: HttpService) {}
      findSomething() {
        return this.httpService.get('http://urldoesntmatter.something').pipe(
           map(response => response.data)
    

    Thanks for your time if you already have a look and tried to resolve the problem.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.