Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Angular 4中将数组合并到Promise中后,将其显示为未定义数组_Javascript_Angular_Promise - Fatal编程技术网

Javascript 在Angular 4中将数组合并到Promise中后,将其显示为未定义数组

Javascript 在Angular 4中将数组合并到Promise中后,将其显示为未定义数组,javascript,angular,promise,Javascript,Angular,Promise,我有一个功能: filterAllComponent(inputdata) { let a=[], b=[],c=[]; a= this.getfilterPlaces(inputdata); b= this.getfilterTransporter(inputdata); c= this.getfilterVehicles(inputdata); let getplaceArray = [], getTransporterArray = [],

我有一个功能:

filterAllComponent(inputdata) {
    let a=[], b=[],c=[];
    a=  this.getfilterPlaces(inputdata);
    b= this.getfilterTransporter(inputdata);
    c= this.getfilterVehicles(inputdata);

    let getplaceArray = [],
  getTransporterArray = [],
  getVehicleArray = [];

let getPlacePromise = function () {
  const self = this;
  return new Promise(function (resolve, reject) {
    getplaceArray = a;
    resolve("got places\n");
  });
};

let getTransporterPromise = function (message) {
  const self = this;
  return new Promise(function (resolve, reject) {
    getTransporterArray =  b
    resolve(message + "got Transporter");
  });
};

let getVehiclePromise = function (message) {
  const self = this;
  return new Promise(function (resolve, reject) {
    getVehicleArray = c
    resolve(message + "got vehicle");
  });
};

getPlacePromise().then(function (result) {
  return getTransporterPromise(result);
}).then(function (result) {
  return getVehiclePromise(result);
}).then(function (result) {
  var AllDropdownValues = getTransporterArray.concat(getVehicleArray).concat(getplaceArray);
  console.log(AllDropdownValues);
});


}
需要使用getVehicleArraygetplaceArraygetTransporterArray关联数组的所有下拉值。 在最终结果中,数组getplaceArray显示未定义。其余的结果显示正确

调用函数:

getfilterTransporter(autocompleteInputData) {

    var k= this.checkRegex(autocompleteInputData);
    this.getfilteredTransporter= this.filterTransporters(k);
    return this.formatTransporterValue(this.getfilteredTransporter);
}
getfilterVehicles(autocompleteInputData) {

  var k= this.checkRegex(autocompleteInputData);
    this.getfilteredVehicle= this.filterVehicles(k);
    return this.formatVehicleValue(this.getfilteredVehicle);
}
getfilterPlaces(autocompleteInputData) {
  if (autocompleteInputData == '' || typeof(autocompleteInputData) == 'object') 
  return null;
  this.placeData.getPlacesFromPig(autocompleteInputData)
  .subscribe(response =>
    return this.formatPigResponse(response);
   }); 

}

对于getfilterPlaces,subscribe()用于从API调用中提取数据。但是当我调用函数filterAllComponent()时,getfilterPLaces没有被填充,但是其他两个函数运行正常。

代码的问题是,订阅中的
返回表达式
没有作为
getfilterPLaces函数的
返回值
返回。这意味着,如果设置了
autocompleteInputData
,并且没有对象,则
getfilterPlaces函数
没有指定的
返回值

问题是,
this.placeData.getPlacesFromPig
似乎是一个异步函数,返回一个
rjxs Observable
或类似的东西。避免此问题的一种方法是使用
wait
运算符和。为此,必须将
getfilterPlaces
filterAllComponent函数
标记为
async
,并使用
a=wait.getfilterPlaces(inputData)
。另外,
getfilterPlaces
需要返回一个
Promise
。假设
this.placeData.getPlacesFromPig
返回一个
rxjs Observable
,代码如下所示:

async function getfilterPlaces(autocompleteInputData) {
  if (autocompleteInputData == '' || typeof(autocompleteInputData) == 'object') 
    return null;
  return this.placeData.getPlacesFromPig(autocompleteInputData)
    .toPromise()
    .then(response => this.formatPigResponse(response));
}


很多。这正是我需要的。工作完美
async function filterAllComponent(inputdata) {
  let a=[], b=[], c=[];
  a = await this.getfilterPlaces(inputdata);
  b = this.getfilterTransporter(inputdata);
  c = this.getfilterVehicles(inputdata);
[...]