Search code examples
node.jsnestjs

HttpService from @nestjs/axios doesn't work on node 18


I have this code on a service of NestJS

import { HttpException, Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { lastValueFrom, catchError, map } from 'rxjs';
import { AdjuntoService } from '../adjunto/adjunto.service';
import { HtmlPdfBodyPost, HtmlPdfBodyQueryPost } from 'src/dtos/aux.dto';
import { CierreContableService } from '../cierre_contable/cierre_contable.service';

@Injectable()
export class FilesService {
  constructor(
    private readonly httpService: HttpService,
    private readonly adjunto: AdjuntoService,
    private readonly cierreContable: CierreContableService,
  ) {}

  async htmlToPdf(query: HtmlPdfBodyQueryPost, body: HtmlPdfBodyPost) {
    const FormData = require('form-data');
    const bodyFormData: FormData = new FormData();

    for (const key in body) {
      bodyFormData.append(key, body[key]);
    }

    const data = await lastValueFrom(
      this.httpService
        .post(process.env.PYPDF.concat('html_pdf'), bodyFormData)
        .pipe(
          map((resp) => resp.data),
          map((data_) => data_),
          catchError((e) => {
            throw new HttpException(e.response.data, e.response.status);
          }),
        ),
    );

    return { data };
  }
}

If I execute it with node 16 it works fine, but with node 18, I get this error:

Cannot read properties of undefined (reading 'data')


Solution

  • The issue was related to https://github.com/axios/axios/issues/3821

    Finally I solved it by doing suggested here https://github.com/axios/axios/issues/3821#issuecomment-1413727575:

    import { setDefaultResultOrder } from "dns";
    setDefaultResultOrder("ipv4first");