javascript - Cannot set property 'baseUrl' of undefined - Stack Overflow

I have generated a javascript client using NSWAG from a ASP .NET Rest API. But I am finding a few error

I have generated a javascript client using NSWAG from a ASP .NET Rest API. But I am finding a few errors when I am trying to call an API method.

The error in cause 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;

In my angular code I am doing:

    var client = AuthClient('http://localhost:14959/');
    var call = client.prototype.login(data);

I have already debugged the application and it enters the function AuthClient and I have also tried to change it to this, but the problem persists:

function AuthClient($http, baseUrl) {
    this.baseUrl = baseUrl;
};

I am also getting this error: Uncaught ReferenceError: exports is not defined I don't know if it has something to do with it or not. If not just ignore.

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 pile (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)

I have generated a javascript client using NSWAG from a ASP .NET Rest API. But I am finding a few errors when I am trying to call an API method.

The error in cause 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;

In my angular code I am doing:

    var client = AuthClient('http://localhost:14959/');
    var call = client.prototype.login(data);

I have already debugged the application and it enters the function AuthClient and I have also tried to change it to this, but the problem persists:

function AuthClient($http, baseUrl) {
    this.baseUrl = baseUrl;
};

I am also getting this error: Uncaught ReferenceError: exports is not defined I don't know if it has something to do with it or not. If not just ignore.

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 pile (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)
Share Improve this question edited Feb 7, 2017 at 18:41 Rui Queirós asked Feb 7, 2017 at 18:25 Rui QueirósRui Queirós 1251 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Inside your AuthClient function, you need to actually return your constructor, and then call it using new:

var AuthClient = (function () {
    // ...

    return AuthClient;
}());

var client = new AuthClient('http://localhost:14959/');

You can probably omit the exports line.

This line is not required:

this.baseUrl = undefined;

If you again have this:

this.baseUrl = baseUrl !== undefined ? baseUrl : "";

Best way to do this is to:

this.baseUrl = '' || this.baseUrl;

And in AuthClient you have two parameters, out of which the second parameter is baseUrl. But when you are setting it, you are using only one parameter.

So define another function inside that and then use the parameter.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745228498a4617571.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信