[英]Getting undefined data when i called a service
So I have a question that i've been trying to solve for hours. 所以我有一个问题,我一直在努力解决数小时。
My problem is that I'm getting the data faster than my services to load. 我的问题是,获取数据的速度比加载服务的速度快。
gen-service GEN-服务
function genEmpId() {
settingsService.getSettings().then(function (data) {
var comId = data.data[0].companyId;
console.log(comId);
var test = comId + ' - ';
return test;
});}
controller 调节器
function genId() {
var data = genService.genEmpId();
console.log(data); // getting the data too fast how to put a callback ?
}
So when my controller load its calling the service but im getting an undefined return value. 因此,当我的控制器加载其调用服务但我得到未定义的返回值时。
try this, In your code you not returning anything and another thing is it's async call to you have to wait until it finishes. 试试看,在您的代码中您什么都不返回,另一件事是它是异步调用,您必须等待它完成。
// gen-service
function genEmpId() {
return settingsService.getSettings().then(function (data) {
var comId = data.data[0].companyId;
console.log(comId);
var test = comId + ' - ';
return test;
});
}
// controller
function genId() {
var data = genService.genEmpId().then(function (data) {
console.log(data);
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.