Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法设置属性';baseUrl';未定义的_Javascript_Angularjs_Typescript_Nswag - Fatal编程技术网

Javascript 无法设置属性';baseUrl';未定义的

Javascript 无法设置属性';baseUrl';未定义的,javascript,angularjs,typescript,nswag,Javascript,Angularjs,Typescript,Nswag,我已经从ASP.NETRESTAPI使用NSWAG生成了一个javascript客户端。但是当我试图调用API方法时,我发现了一些错误 原因中的错误为:TypeError:无法设置未定义的属性“baseUrl” var AuthClient = (function () { function AuthClient($http, baseUrl) { this.baseUrl = undefined; this.http = null; th

我已经从ASP.NETRESTAPI使用NSWAG生成了一个javascript客户端。但是当我试图调用API方法时,我发现了一些错误

原因中的错误为:TypeError:无法设置未定义的属性“baseUrl”

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;
在我的角度代码中,我正在做:

    var client = AuthClient('http://localhost:14959/');
    var call = client.prototype.login(data);
我已经调试了应用程序,它进入了函数AuthClient,我也尝试将其更改为此,但问题仍然存在:

function AuthClient($http, baseUrl) {
    this.baseUrl = baseUrl;
};
我也遇到了这个错误:
uncaughtreferenceerror:exports没有定义
我不知道它是否与此有关。如果不是,那就忽略吧

错误跟踪:

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)
TypeError:无法设置未定义的属性“baseUrl”
在AuthClient(RenterLogApiBackendClient.js:19)
在b.$scope.login(HomeController.js:16)
fn时(编译时求值(angular.min.js:1),:4:206)
在b(angular.js:16123)
在e(angular.js:26490)
以b.$eval计算(angular.js:17913)
b.$apply(angular.js:18013)
在HTMLFormElement。(angular.js:26495)
在HTMLFormElement.dispatch(jquery-3.1.1.min.js:3)
位于HTMLFormElement.q.handle(jquery-3.1.1.min.js:3)

此行不是必需的:

this.baseUrl = undefined;
如果您再次遇到此问题:

this.baseUrl = baseUrl !== undefined ? baseUrl : "";
最好的方法是:

this.baseUrl = '' || this.baseUrl;
AuthClient
中有两个参数,其中第二个参数是
baseUrl
。但是当您设置它时,您只使用了一个参数


因此,在该函数中定义另一个函数,然后使用该参数。

AuthClient
函数中,您需要实际返回构造函数,然后使用
new
调用它:

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

    return AuthClient;
}());

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

您可能可以省略导出行。

正如我所说,我只尝试了以下功能:AuthClient($http,baseUrl){this.baseUrl=baseUrl;};得到了同样的错误建议是倒退的;应该是
this.baseUrl=baseUrl | |“”
@RuiQueirós是的,这是因为在
AuthClient
中有两个参数。而是在
AuthClient
中定义另一个方法,并将
baseUrl
作为参数。为
post
callIt调用该方法没有关系,同样的事情也会发生。我将用完整的堆栈更新我的问题。