2017-05-16 4 views
0

フロントエンドにはAngularJSを使用し、バックエンドにはJava(Spring)を使用するアプリケーションに取り組んでいます。アプリケーションはhttp://localhost:8080/Sprint002/restpatent/をリクエストして、ApacheとTomcatの両方を実行しています(ポート80のApache)。未定義のプロパティを読み取ることができません - コードまたはサーバーで問題がありますか?

エラーTypeError: Cannot read property 'fetchAllUsers' of undefinedが返されます。だから私が見ることができるのは、オブジェクトが見つからないということです。 ApacheとTomcatを同時に実行することに問題はありますか?そうでない場合は、理由は何ですか?

app.controller('UserController', ['UserService', function($scope, UserService) { 


var self = this; 
self.user={id:null,username:'',address:'',email:''}; 
self.users=[]; 
self.submit = submit; 
self.edit = edit; 
self.remove = remove; 
self.reset = reset; 
console.log('hello') 


fetchAllUsers(); 

function fetchAllUsers(){ 
    console.log('hello2'); 
    UserService.fetchAllUsers() 
     .then(
     function(d) { 
      self.users = d; 
     }, 
     function(errResponse){ 
      console.error('Error while fetching Users'); 
     } 
    ); 
} 

app.factory('UserService', ['$http', '$q', function($http, $q){ 
    console.log('hello3') 

var REST_SERVICE_URI = 'http://localhost:8080/Sprint002/restpatent/'; 

var factory = { 
    fetchAllUsers: fetchAllUsers, 
    createUser: createUser, 
    updateUser:updateUser, 
    deleteUser:deleteUser 
}; 

return factory; 

function fetchAllUsers() { 
    var deferred = $q.defer(); 
     console.log('hello4') 
    $http.get(REST_SERVICE_URI) 
     .then(
     function (response) { //success 
      deferred.resolve(response.data); //processes the data 
     }, 
     function(errResponse){ //error 
      console.error('Error while fetching Users'); 
      deferred.reject(errResponse); //rejects the data 
     } 
    ); 
    return deferred.promise; 
} 

[{"id":1,"applicationNumber":"N8167625","clientRef":"TER-34421","costToRenew":"534","renewalDueDate":"30/09/2017","basketStatus":"Cannot add to Basket"},{"id":2,"applicationNumber":"W71679234","clientRef":"Breitling","costToRenew":"5367","renewalDueDate":"30/11/2017","basketStatus":"Add to Basket"},{"id":3,"applicationNumber":"T7167921","clientRef":"Rolex","costToRenew":"8341","renewalDueDate":"30/05/2017","basketStatus":"In Basket"},{"id":4,"applicationNumber":"uk167444","clientRef":"Timex","costToRenew":"8341","renewalDueDate":"30/02/2017","basketStatus":"Add to Basket"}] 
+0

あなたはこの工場をどこに注入するのですか? – quirimmo

+0

今更新されました –

+0

あなたは注入中にエラーが発生しました。答え、それを試してみましょう – quirimmo

答えて

2

変更この:これに

app.controller('UserController', ['UserService', function($scope, UserService) 

app.controller('UserController', ['$scope', 'UserService', function($scope, UserService) 
+0

OMG。それだった!問題は何でしたか? '$ scope'は含まれていなかったと私は理解していますが、大括弧の構文を含む理由は依存関係を含めることだと思っていました。私はそれが完全に間違っている?どうもありがとう! –

+1

この構文を使用するときは、角括弧内の順序と丸括弧内の順序が同じであることに注意する必要があります。このソリューションは、コードを醜く/縮小したい場合には優れていますが、この冗長性を取り除き、コードをデプロイするときにng-annotateを使用して自動的に行うこともできます。 この最後の解決策は、私があなたに提案することができるものです。これは、直前にコードを縮小/醜くするためです。それは理にかなっていますか? – quirimmo

+0

角括弧の構文を使用する場合は、関数に渡されたすべてのパラメータを必ず含めてください。 –

関連する問題