2017-04-02 75 views
0

試圖從服務中加載數據,並且收到以下錯誤消息:Error: undefined is not an object (evaluating 'ProfileService.getProfile(1).then錯誤:未定義不是對象(正在評估'ProfileService.getProfile(1).then')

ProfileController可

錯誤發生在這裏...

vm.user = {}; 

vm.loadProfile = function() { 
    ProfileService.getProfile(1).then(function(profileData) { 
    console.log(profileData); 
    vm.user = profileData; 
    }) 
}; 

vm.loadProfile() 

ProfileService

function getProfile(id) { 
    var profile = []; 

    $log.debug('Getting profile with id: ' + id); 
    StubFactory.getProfileData().then(function(result) { 
    for(var i = 0; i < result.data.profiles.length; i++) { 
     if(result.data.profiles[i].id === id) { 
     profile = result.data.profiles[i]; 
     } 
    } 

    $log.debug('Getting contacts for profile with id: ' + id); 
    profile.contacts = []; 
    StubFactory.getContactsData().then(function(result) { 
     for(i = 0; i < profile.contactMapper.length; i++) { 
     for(var j = 0; j < result.data.contacts.length; j++) { 
      if(profile.contactMapper[i] === result.data.contacts[j].id) { 
      profile.contacts.push(result.data.contacts[j]); 
      } 
     } 
     } 
    }); 

    $log.debug('Getting documents for profile with id: ' + id); 
    profile.documents = []; 
    StubFactory.getDocumentsData().then(function(result) { 
     for(i = 0; i < profile.documentMapper.length; i++) { 
     for(var j = 0; j < result.data.documents.length; j++) { 
      if(profile.documentMapper[i] === result.data.documents[j].id) { 
      profile.documents.push(result.data.documents[j]); 
      } 
     } 
     } 
    }); 
    $log.debug('Completed load for profile with id: ' + id); 
    $log.info(profile); 
    return profile; 
    }); 
} 

StubFactory

function getContactsData() { 
    return $http.get('data/contacts.json'); 

} 

function getProfileData() { 
    return $http.get('data/profiles.json'); 
} 

function getDocumentsData() { 
    return $http.get('data/documents.json'); 
} 

日誌是如下...

[Warning] Unexpected CSS token: : (main.css, line 4919) 
[Warning] Unexpected CSS token: : (main.css, line 8199) 
[Warning] Unexpected CSS token: : (main.css, line 8207) 
[Debug] Getting profile with id: 1 (angular.js, line 14326) 
[Error] Error: undefined is not an object (evaluating 'ProfileService.getProfile(1).then') 
[email protected]://localhost:9000/scripts/controllers/profile.js:17:35 
http://localhost:9000/scripts/controllers/profile.js:22:19 
[email protected]://localhost:9000/bower_components/angular/angular.js:10717:40 
[email protected]://localhost:9000/bower_components/angular/angular.js:9594:45 
[email protected]://localhost:9000/bower_components/angular/angular.js:8903:23 
[email protected]://localhost:9000/bower_components/angular/angular.js:8768:45 
[email protected]://localhost:9000/bower_components/angular-route/angular-route.js:1222:11 
[email protected]://localhost:9000/bower_components/angular/angular.js:10274:15 
[email protected]://localhost:9000/bower_components/angular/angular.js:9663:23 
[email protected]://localhost:9000/bower_components/angular/angular.js:8903:23 
[email protected]://localhost:9000/bower_components/angular/angular.js:8768:45 
[email protected]://localhost:9000/bower_components/angular-route/angular-route.js:1171:36 
[email protected]://localhost:9000/bower_components/angular/angular.js:18343:33 
http://localhost:9000/bower_components/angular-route/angular-route.js:733:40 
[email protected]://localhost:9000/bower_components/angular/angular.js:16689:39 
http://localhost:9000/bower_components/angular/angular.js:16733:39 
[email protected]://localhost:9000/bower_components/angular/angular.js:17827:36 
[email protected]://localhost:9000/bower_components/angular/angular.js:18125:31 
[email protected]://localhost:9000/bower_components/angular/angular.js:12233:53 
[email protected]://localhost:9000/bower_components/angular/angular.js:12459:15 
[email protected]://localhost:9000/bower_components/angular/angular.js:12387:24 – "<div ng-view=\"\" class=\"ng-scope\">" 
    (anonymous function) (angular.js:10859) 
    invokeLinkFn (angular.js:10276) 
    nodeLinkFn (angular.js:9663) 
    compositeLinkFn (angular.js:8903) 
    publicLinkFn (angular.js:8768) 
    update (angular-route.js:1171) 
    $broadcast (angular.js:18343) 
    (anonymous function) (angular-route.js:733) 
    processQueue (angular.js:16689) 
    (anonymous function) (angular.js:16733) 
    $digest (angular.js:17827) 
    $apply (angular.js:18125) 
    done (angular.js:12233) 
    completeRequest (angular.js:12459) 
    requestLoaded (angular.js:12387) 
[Debug] Getting contacts for profile with id: 1 (angular.js, line 14326) 
[Debug] Getting documents for profile with id: 1 (angular.js, line 14326) 
[Debug] Completed load for profile with id: 1 (angular.js, line 14326) 
[Info] {id: 1, firstName: "Robert", lastName: "Emery", address: "95 Guild Street, London, N14 3UF", type: "tenant", …} (profile.js, line 115) 

profile.js, line 115是代碼中ProfileService以下行:$log.info(profile);

+0

請驗證您的數據,檢查'.length'之前,因爲如果它是'null',那麼你會得到錯誤。 – anoop

+0

@anoop數據不爲空,我可以看到服務中的日誌,它只是不起作用,它回到了控制器 – methuselah

+0

控制器中的onload方法似乎在接收實際數據之前觸發 – methuselah

回答

2

您的問題與promise的異步行爲有關,這很好。因此,爲了讓您的數據,你有2種方式:

第一方式是鏈.then事件,並從去年promise result調用callbackfunction。 代碼如下所示:

ProfileController可

vm.user = {}; 
vm.loadProfile = function() { 
    ProfileService.getProfile(1, callBackfunc); // pass callback function 
}; 

function callBackfunc(profileData){ 
    vm.user = profileData; 
} 
vm.loadProfile(); 

ProfileService

function getProfile(id, callBackfunc) { 
     var profile = []; 
     // chain .then events 
     $log.debug('Getting profile with id: ' + id); 
     StubFactory.getProfileData().then(function(result) { 
     for(var i = 0; i < result.data.profiles.length; i++) { 
      if(result.data.profiles[i].id === id) { 
      profile = result.data.profiles[i]; 
      } 
     } 

     $log.debug('Getting contacts for profile with id: ' + id); 
     profile.contacts = []; 
     StubFactory.getContactsData().then(function(result) { 
      for(i = 0; i < profile.contactMapper.length; i++) { 
      for(var j = 0; j < result.data.contacts.length; j++) { 
       if(profile.contactMapper[i] === result.data.contacts[j].id) { 
       profile.contacts.push(result.data.contacts[j]); 
       } 
      } 
      } 

     $log.debug('Getting documents for profile with id: ' + id); 
     profile.documents = []; 
     StubFactory.getDocumentsData().then(function(result) { 
      for(i = 0; i < profile.documentMapper.length; i++) { 
      for(var j = 0; j < result.data.documents.length; j++) { 
       if(profile.documentMapper[i] === result.data.documents[j].id) { 
       profile.documents.push(result.data.documents[j]); 
       } 
      } 
      } 
     $log.debug('Completed load for profile with id: ' + id); 
     $log.info(profile); 
     callBackfunc(profile); // callback function called. 
     }); 
     }); 
    }); 
} 

的方法是使用$q服務,並利用它的$q.all()方法。

那麼你的代碼會像:

let promises = [promisegetProfileData(), promisegetContactsData(), promisegetDocumentsData()]; 

$q.all(promises).then((values) => { 
    var profile = []; 
    // assign ProfileData values[0] 
    // assign ContactData values[1] 
    // assign DocumentData values[2] 

    callBackFunc(profile); 
}); 

Read more here for $q

+0

@methuselah:它工作了嗎? – anoop

相關問題