2017-02-07 1 views
1

ASP .NET Rest API에서 NSWAG를 사용하여 자바 스크립트 클라이언트를 생성했습니다. 하지만 API 메소드를 호출하려고 할 때 몇 가지 오류가 발생합니다.정의되지 않은 'baseUrl'속성을 설정할 수 없습니다.

오류의 원인은 다음과 같습니다. TypeError : undefined의 'baseUrl'속성을 설정할 수 없습니다.

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

가 이미 응용 프로그램을 디버깅하고이 기능 AuthClient를 입력하고 나는이로 변경하려고했지만 문제가 지속 : 내 각 코드에서

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; 

내가 뭐하는 거지 :

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

나는이 오류가 무엇입니까 : Uncaught ReferenceError: exports is not defined 그것 또는하지 할 수있는 뭔가가 있는지 모르겠어요. 그냥 무시하지 않으면.

오류 추적 :이 줄은 필요하지 않습니다

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) 

답변

1

, 당신은 실제로 당신의 생성자를 반환해야하고 new를 사용하여 전화 : 당신은 아마 exports 라인을 생략 할 수 있습니다

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

    return AuthClient; 
}()); 

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

.

0

:

this.baseUrl = baseUrl !== undefined ? baseUrl : ""; 
이 작업을 수행하는

가장 좋은 방법은에 :

this.baseUrl = undefined; 

다시이있는 경우

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

그리고 AuthClient에는 두 개의 매개 변수가 있으며 그 중 두 번째 매개 변수는 baseUrl입니다. 그러나 설정을 할 때 하나의 매개 변수 만 사용하고 있습니다.

그 안에 다른 함수를 정의한 다음 매개 변수를 사용하십시오. 당신의 AuthClient 기능 내부

+0

내가 말했듯이, 나는 이것만을 시도했다 : function AuthClient ($ http, baseUrl) { this.baseUrl = baseUrl; }; 동일한 오류가 발생했습니다. –

+0

제안 사항이 거꾸로 작성되었습니다. 'this.baseUrl = baseUrl ||이어야한다. ";". – mc10

+0

@ RuiQueirós 예, AuthClient에 두 개의 매개 변수가 있기 때문입니다. 대신'authClient' 내부에'baseUrl'을 매개 변수로하는 다른 메소드를 정의하십시오. 그리고'post' 호출을위한 메소드 호출 –

관련 문제