简体   繁体   English

致电服务时获取未定义的数据

[英]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.

相关问题 收到TypeError:尝试包含我用AngularJS制作的AuthInterceptor服务时,无法读取未定义的属性“ data” - Getting TypeError: Cannot read property 'data' of undefined, when trying to include an AuthInterceptor service I made with AngularJS 在从 html 调用的服务上获取未定义 - Getting undefined on service called from html 角度2:从服务返回数据时未定义 - Angular 2: Getting undefined when return data from service 第二个.then()on promise被调用,数据为undefined - Second .then() on promise is getting called with data as undefined 测试服务时未调用 NestJS UseFilter - NestJS UseFilter not getting called when testing Service 尝试在确认中显示数据时,我一直得到不确定的信息 - I keep getting an undefined when trying to show data in a confirmation 尝试从标签获取数据时获取未定义的值 - Getting an undefined value when I try to get data from a label 在 html 中获取数据属性时,我得到了未定义的数据集 - I got undefined dataset, when getting data attribute in html 从重复调用的promise返回服务中获取最新数据 - Getting the latest data from a promise returning service called repeatedly 从API请求数据时获取“未定义” - Getting 'undefined' when requesting data from API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM