I recently created a JavaScript client using NSWAG from an ASP .NET Rest API. However, I am encountering some errors when attempting to call an API method.
The specific error message I am receiving is: TypeError: Cannot set property 'baseUrl' of undefined.
var AuthClient = (function () {
function AuthClient($http, baseUrl) {
this.baseUrl = undefined;
this.http = null;
this.jsonParseReviver = undefined;
this.http = $http;
this.baseUrl = baseUrl !== undefined ? baseUrl : "";
};
AuthClient.prototype.login = function (auth) {
var _this = this;
var url_ = this.baseUrl + "/api/v1/auth/login";
var content_ = JSON.stringify(auth ? auth.toJS() : null);
return this.http({
url: url_,
method: "POST",
data: content_,
transformResponse: [],
headers: {
"Content-Type": "application/json; charset=UTF-8",
"Accept": "application/json; charset=UTF-8"
}
}).then(function (response) {
return _this.processLogin(response);
}, function (response) {
if (response.status)
return _this.processLogin(response);
throw response;
});
};
...}());
exports.AuthClient = AuthClient;
My angular code includes:
var client = AuthClient('http://localhost:14959/');
var call = client.prototype.login(data);
After debugging the application, I noticed that it enters the function AuthClient. I even attempted to change it to this, but the issue persists:
function AuthClient($http, baseUrl) {
this.baseUrl = baseUrl;
};
Another error that I am encountering is:
Uncaught ReferenceError: exports is not defined
. I am unsure if this is related to the current issue or not, but thought it was worth mentioning.
Error trace:
TypeError: Cannot set property 'baseUrl' of undefined
at AuthClient (RenterLogApiBackendClient.js:19)
at b.$scope.login (HomeController.js:16)
at fn (eval at compile (angular.min.js:1), <anonymous>:4:206)
at b (angular.js:16123)
at e (angular.js:26490)
at b.$eval (angular.js:17913)
at b.$apply (angular.js:18013)
at HTMLFormElement.<anonymous> (angular.js:26495)
at HTMLFormElement.dispatch (jquery-3.1.1.min.js:3)
at HTMLFormElement.q.handle (jquery-3.1.1.min.js:3)