我有一个功能:

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);
});


}


需要使用getVehicleArray,getplaceArray和getTransporterArray合并数组AllDropdownValues。
在最终结果中,数组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,但是其他两个函数都正常。

最佳答案

您的代码的问题在于,订阅中的return expression没有作为return valuegetfilterPlaces function退还给您。这意味着,如果设置了getfilterPlaces function并且没有对象,则您的return value没有指定的autocompleteInputData

问题是this.placeData.getPlacesFromPig似乎是一个异步函数,并且返回rjxs Observable或类似的东西。避免此问题的一种方法是使用await运算符和async function declaration。为此,您必须将getfilterPlacesfilterAllComponent functions标记为async,然后使用a = await this.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);
[...]

09-20 10:07