gpt4 book ai didi

javascript - 无法设置未定义的属性 'baseUrl'

转载 作者:太空宇宙 更新时间:2023-11-04 16:03:24 28 4
gpt4 key购买 nike

我已经使用 NSWAG 从 ASP .NET Rest API 生成了一个 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;

在我的 Angular 代码中,我正在做:

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

我已经调试了应用程序,它进入了 AuthClient 函数,我也尝试将其更改为这样,但问题仍然存在:

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

我也收到此错误:Uncaught ReferenceError:未定义导出我不知道它是否与此有关。如果不是就忽略吧。

错误跟踪:

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)

最佳答案

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

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

return AuthClient;
}());

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

您可以省略 exports 行。

关于javascript - 无法设置未定义的属性 'baseUrl',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42097232/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com