TypeError: MyClass.MyAPI is not a constructor

Viewed 192

I am trying to implement a javascript API generated by the openapi-generator to my react, meteor projekt. I've generated, installed and linked the generated API without problem. But when I try to use the API functionality on a server side function, I get the following error in my serverlog:

I20210617-08:11:53.301(0)? error: ERROR: TypeError: MyClass.MyAPI is not a constructor

The generated node_modules name from the openapi-generator is "my_class" and the object I want to instanciate is from the "MyAPI.js" file.

In meteor projekt I have this addDocument.js, which should use the API, loolks like the following:

import MyClass from 'my_class';

const myAPICall = () => {
  var apiInstance= new MyClass.MyAPI(); //This line causes the error

  var callback = function(error, data, response) {
    if (error) {
      console.error(error);
    } else {
      console.log('API called successfully. Returned data: ' + data);
    }
  };
  apiInstance.myAPI(callback);
  console.log(apiInstance);
};

export default function addDocument(meetingID) {
  try {
    myAPICall();
  } catch (error) {
    Logger.error(`ERROR: ${error}`);
  }
}

This error seems strange to me, because if I type "MyClass." and press CTRL+Space in my IDE, VSCode suggests me "MyClass.MyAPI" as a valid constructor.

The generated MyAPI.js looks like the following:

(function(root, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['ApiClient', 'model/Error', 'model/MyAPIResponse'], factory);
  } else if (typeof module === 'object' && module.exports) {
    // CommonJS-like environments that support module.exports, like Node.
    module.exports = factory(require('../ApiClient'), require('../model/Error'), require('../model/MyAPIResponse'));
  } else {
    // Browser globals (root is window)
    if (!root.MyClass) {
      root.MyClass= {};
    }
    root.MyClass.MyAPI = factory(root.myClass.ApiClient, root.MyClass.Error, root.MyClass.MyAPIResponse);
  }
}(this, function(ApiClient, Error, MyAPIResponse) {
  'use strict';

  /**
   * MyAPI service.
   * @module api/MyAPI
   * @version 1.0.0
   */

  /**
   * Constructs a new MyAPI. 
   * @alias module:api/MyAPI
   * @class
   * @param {module:ApiClient} [apiClient] Optional API client implementation to use,
   * default to {@link module:ApiClient#instance} if unspecified.
   */
  var exports = function(apiClient) {
    this.apiClient = apiClient || ApiClient.instance;


    /**
     * Callback function to receive the result of the myAPI operation.
     * @callback module:api/MyAPI~myAPICallback
     * @param {String} error Error message, if any.
     * @param {module:model/MyAPIResponse} data The data returned by the service call.
     * @param {String} response The complete HTTP response.
     */

    /**
     * myAPI
     * @param {module:api/MyAPI~myAPICallback} callback The callback function, accepting three arguments: error, data, response
     * data is of type: {@link module:model/MyAPIResponse}
     */
    this.myAPI= function(callback) {
      var postBody = null;


      var pathParams = {
      };
      var queryParams = {
      };
      var collectionQueryParams = {
      };
      var headerParams = {
      };
      var formParams = {
      };

      var authNames = [];
      var contentTypes = [];
      var accepts = ['application/json', 'application/xml'];
      var returnType = MyAPIResponse;

      return this.apiClient.callApi(
        '/myAPI', 'POST',
        pathParams, queryParams, collectionQueryParams, headerParams, formParams, postBody,
        authNames, contentTypes, accepts, returnType, callback
      );
    }
  };

  return exports;
}));

npm Version: 6.13.4

node Version: v8.17.0

meteor Version: Meteor 1.8

Thanks in advance. Regards

0 Answers
Related