Home > other >  ERROR [ExceptionsHandler] TypeError: Cannot read properties of undefined (reading 'post')
ERROR [ExceptionsHandler] TypeError: Cannot read properties of undefined (reading 'post')

Time:10-07

I am practising to convert soap api response to rest api response. I have used nodejs to do this before and it worked. Now I am trying to use nestJS but it keeps throwing an error: TypeError: Cannot read properties of undefined (reading 'post') <> Oops something went wrong. Please try again later {} which is coming from the catch block of my code in remote.ts when axios calls the POST method. I dropped and used soap library instead but it gave createAsync error. I'm confused right now. What am I doing wrong? I am back to using axios yet again. I have pasted my codes below. There's probably something I am doing wrong which is not obvious to me.

parser.ts

import * as jsonxml from 'jsontoxml';
import * as xml2js from 'xml2js';
import { promisify } from 'util';

const parseString = promisify(xml2js.parseString);

const promisfiedParseString = promisify(parseString as any);

export default class Parser {
  static parseJSONBodyToXML(jsonArgument: any) {
    return jsonxml(jsonArgument, { html: true });
  }

  static async convertXMLToJSON(xmlMessage: xml2js.convertableToString) {
    const options = { trim: true, explicitArray: false, explicitRoot: false };
    const result = await promisfiedParseString(xmlMessage, options);
    return result;
  }
}

formatter.ts

import Parser from './parser';

export default class Formatter {
  static convertJsonToSoapRequest(jsonArguments: any) {
    const soapBody = Parser.parseJSONBodyToXML(jsonArguments);

    return `
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tempuri.org/">
      <soap:Header/>
      <soap:Body>
        ${soapBody}
      </soap:Body>
    </soap:Envelope>
    `;
  }
}

remote.ts

import Formatter from './formatter';
import axios from 'axios';
import Parser from './parser';

const url = 'http://www.dneonline.com/calculator.asmx';

export default class Remote {
  static async multipleTwoOperands(operandA: any, operandB: any) {
    try {
      const payload = {
        Multiply: {
          intA: operandA,
          intB: operandB,
        },
      };

      const headers = {
        headers: {
          'Content-Type': 'text/xml; charset=utf-8',
          SOAPAction: 'http://tempuri.org/Multiply',
        },
      };

      const args = Formatter.convertJsonToSoapRequest(payload);
      console.log(args);
      let remoteResponse = await axios.post(url, args, headers);
      remoteResponse = await Parser.convertXMLToJSON(remoteResponse);

      console.log(remoteResponse);
    } catch (err) {
      throw new Error(
        `${err} <> Oops something went wrong. Please try again later ${JSON.stringify(
          err,
        )}`,
      );
    }
  }
}

conversion.service.ts

import { Injectable } from '@nestjs/common';
import Remote from './util/remote';

@Injectable()
export class AppService {
  async getRemote(firstNumber: string, secondNumber: string): Promise<any> {
    const firstNum = parseInt(firstNumber);
    const secondNum = parseInt(secondNumber);

    return await Remote.multipleTwoOperands(firstNum, secondNum);
  }
}

conversion.controller.ts

import { Body, Controller, Post } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Post('remote')
  async getRemote(
    @Body('firstNumber') firstNumber: string,
    @Body('secondNumber') secondNumber: string,
  ): Promise<any> {
    console.log(firstNumber, secondNumber);

    return await this.appService.getRemote(firstNumber, secondNumber);
  }
}

axios.get also gives the same error but with 'get' instead. No axios method seems to be working.

CodePudding user response:

I believe you should import axios like so:

import * as axios from 'axios';

or enable the esModuleInterop option

CodePudding user response:

lastest version of axios has bug. try to use older version.

I change

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

To this and it work

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>
  • Related