Javascript 错误:undefined不是对象(评估';ProfileService.getProfile(1)。然后';)
尝试从服务加载数据时,我收到以下错误消息:Javascript 错误:undefined不是对象(评估';ProfileService.getProfile(1)。然后';),javascript,angularjs,Javascript,Angularjs,尝试从服务加载数据时,我收到以下错误消息:错误:undefined不是对象(正在评估'ProfileService.getProfile(1)。然后) ProfileController vm.user = {}; vm.loadProfile = function() { ProfileService.getProfile(1, callBackfunc); // pass callback function }; function callBackfunc(profileData
错误:undefined不是对象(正在评估'ProfileService.getProfile(1)。然后
)
ProfileController
vm.user = {};
vm.loadProfile = function() {
ProfileService.getProfile(1, callBackfunc); // pass callback function
};
function callBackfunc(profileData){
vm.user = profileData;
}
vm.loadProfile();
错误发生在这里
vm.user = {};
vm.loadProfile = function() {
ProfileService.getProfile(1).then(function(profileData) {
console.log(profileData);
vm.user = profileData;
})
};
vm.loadProfile()
档案服务
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;
});
}
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.
});
});
});
}
日志如下
[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')
loadProfile@http://localhost:9000/scripts/controllers/profile.js:17:35
http://localhost:9000/scripts/controllers/profile.js:22:19
$controllerInit@http://localhost:9000/bower_components/angular/angular.js:10717:40
nodeLinkFn@http://localhost:9000/bower_components/angular/angular.js:9594:45
compositeLinkFn@http://localhost:9000/bower_components/angular/angular.js:8903:23
publicLinkFn@http://localhost:9000/bower_components/angular/angular.js:8768:45
link@http://localhost:9000/bower_components/angular-route/angular-route.js:1222:11
invokeLinkFn@http://localhost:9000/bower_components/angular/angular.js:10274:15
nodeLinkFn@http://localhost:9000/bower_components/angular/angular.js:9663:23
compositeLinkFn@http://localhost:9000/bower_components/angular/angular.js:8903:23
publicLinkFn@http://localhost:9000/bower_components/angular/angular.js:8768:45
update@http://localhost:9000/bower_components/angular-route/angular-route.js:1171:36
$broadcast@http://localhost:9000/bower_components/angular/angular.js:18343:33
http://localhost:9000/bower_components/angular-route/angular-route.js:733:40
processQueue@http://localhost:9000/bower_components/angular/angular.js:16689:39
http://localhost:9000/bower_components/angular/angular.js:16733:39
$digest@http://localhost:9000/bower_components/angular/angular.js:17827:36
$apply@http://localhost:9000/bower_components/angular/angular.js:18125:31
done@http://localhost:9000/bower_components/angular/angular.js:12233:53
completeRequest@http://localhost:9000/bower_components/angular/angular.js:12459:15
requestLoaded@http://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)
[警告]意外的CSS标记::(main.CSS,第4919行)
[警告]意外的CSS标记::(main.CSS,第8199行)
[警告]意外的CSS标记::(main.CSS,第8207行)
[Debug]获取id为1的配置文件(angular.js,第14326行)
[错误]错误:undefined不是对象(正在评估'ProfileService.getProfile(1)。然后')
loadProfile@http://localhost:9000/scripts/controllers/profile.js:17:35
http://localhost:9000/scripts/controllers/profile.js:22:19
$controllerInit@http://localhost:9000/bower_components/angular/angular.js:10717:40
nodeLinkFn@http://localhost:9000/bower\u components/angular/angular.js:9594:45
compositeLinkFn@http://localhost:9000/bower\u components/angular/angular.js:8903:23
publicLinkFn@http://localhost:9000/bower_components/angular/angular.js:8768:45
link@http://localhost:9000/bower_components/angular route/angular route.js:1222:11
invokeLinkFn@http://localhost:9000/bower\u components/angular/angular.js:10274:15
nodeLinkFn@http://localhost:9000/bower_components/angular/angular.js:9663:23
compositeLinkFn@http://localhost:9000/bower\u components/angular/angular.js:8903:23
publicLinkFn@http://localhost:9000/bower_components/angular/angular.js:8768:45
update@http://localhost:9000/bower_components/angular route/angular route.js:1171:36
$broadcast@http://localhost:9000/bower_components/angular/angular.js:18343:33
http://localhost:9000/bower_components/angular-路线/角度路线.js:733:40
processQueue@http://localhost:9000/bower_components/angular/angular.js:16689:39
http://localhost:9000/bower_components/angular/angular.js:16733:39
$digest@http://localhost:9000/bower_components/angular/angular.js:17827:36
$apply@http://localhost:9000/bower_components/angular/angular.js:18125:31
done@http://localhost:9000/bower_components/angular/angular.js:12233:53
completeRequest@http://localhost:9000/bower_components/angular/angular.js:12459:15
requestLoaded@http://localhost:9000/bower\u components/angular/angular.js:12387:24–“
(匿名函数)(angular.js:10859)
invokeLinkFn(angular.js:10276)
nodeLinkFn(angular.js:9663)
compositeLinkFn(angular.js:8903)
publicLinkFn(angular.js:8768)
更新(angular route.js:1171)
$broadcast(angular.js:18343)
(匿名函数)(angular route.js:733)
processQueue(angular.js:16689)
(匿名函数)(angular.js:16733)
$digest(angular.js:17827)
$apply(angular.js:18125)
完成(angular.js:12233)
completeRequest(angular.js:12459)
requestLoaded(angular.js:12387)
[调试]获取id为1的个人资料的联系人(angular.js,第14326行)
[Debug]获取id为1的概要文件的文档(angular.js,第14326行)
[Debug]id为1的配置文件的加载已完成(angular.js,第14326行)
[信息]{id:1,名:“罗伯特”,姓“埃莫里”,地址:“伦敦公会街95号,N14 3UF”,类型:“租户”,…}(profile.js,第115行)
profile.js,第115行是ProfileService
中的以下代码行:$log.info(profile);
您的问题与promise的异步行为有关,这很好。因此,要获取数据,您有两种方法:
首先方法是链接。然后
事件并从最后一个承诺结果
调用callbackfunction
。
代码如下所示:
ProfileController
vm.user = {};
vm.loadProfile = function() {
ProfileService.getProfile(1, callBackfunc); // pass callback function
};
function callBackfunc(profileData){
vm.user = profileData;
}
vm.loadProfile();
档案服务
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;
});
}
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.
});
});
});
}
在检查.length
之前,请验证您的数据,因为如果为null
,则您将得到错误。@操作数据不是null,我可以在服务中看到日志,它只是不起作用,它会返回到控制器。控制器中的onload方法似乎在接收实际数据之前触发ProfileService
被注入您的控制器?是的,我已将日志添加到问题中