【发布时间】:2020-11-05 21:51:13
【问题描述】:
我正在尝试调用两个API,但它们一个接一个地被调用,因此我在不必要地浪费时间。如何同时调用两者并帮助我节省时间。
this.data = await this.processService.workflowAPI1('activiti$requisitionandpo', 'COMPLETED').then((d: any) => {
return d;
})
this.APIresponse = await this.processService.workflowAPI(selected_actID).then((d: any) => {
console.log(d)
this.spinner.hide();
this.showTable = true;
return d;
})
编辑:感谢您回答我的问题,我尝试了所有方法,前提是所有方法都有效,但我在调用时遇到问题
this.data 在每个。是异步的问题吗?
this.data.forEach(element => {
element['properties'] = element.properties.map;
columns.forEach(column => {
if (column.header == 'PO Number') {
if (column.hasMulValue) {
column.value_list.forEach(value => {
var column_value = this.adminService.loadColumnValue(value, element);
if (column_value != null && column_value != '0') {
element['po_num'] = column_value;
}
})
}
}
})
// element1.po_num = element1.properties.map.aclrq_poNum
var x = new Date(element.properties.completionDate);
var y = new Date("Oct 18, 2020");
if ((element.taskType == "aclrq:PreparePo") && (x > y)) {
arraynew.push(element)
}
})
vendor.js:69393 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'forEach' of undefined
TypeError: Cannot read property 'forEach' of undefined
【问题讨论】:
-
你在哪里调用 forEach
-
在一个不同的函数中,我在一个函数中加载了两个 api,在同一个函数中,我正在调用另一个具有
forEach的函数 -
等待响应的函数是异步的,因此也会返回一个 Promise。您需要在调用第二个函数(使用 forEach)之前等待第一个函数(使用 async-await)的 Promise,或者在第一个函数的 then 回调中调用第二个函数
-
如果我把所有的东西都放在一个异步函数中,面临异步问题。在这样做之前,我的情况一切正常。我只是想同时调用两个 api。
标签: angular typescript