我正在尝试重组一些代码。现在看起来像这样:
async function myfunction(ref) {
var r1 = await getReport(ref, "queue", "hour", "2018-10-03", "2018-10-04", "pageviews", "page");
var r2 = await getReport(r1.reportID, "get").then(r2 => reportHandler(r1.reportID, r2, 0));
return r2;
}
const data= await myFunction(ref);
if(data!=null && data.report.data!=null){
const result = data.report.data.reduce((r, e) => {
e.breakdownTotal.forEach(el => {
let key = "data";
if(!r[key]) r[key] = []
r[key].push(...el.counts)
})
return r;
}, {})
}
但是,我想将其重组为单个函数,如下所示:
async function myfunction(ref) {
var r1 = await getReport(ref, "queue", "hour", "2018-10-03", "2018-10-04", "pageviews", "page");
var r2 = await getReport(r1.reportID, "get").then(r2 => reportHandler(r1.reportID, r2, 0));
if(r2!=null && r2.report.data!=null) {
const result = r2.report.data.reduce((r, e) => {
e.breakdownTotal.forEach(el => {
let key = "data";
if(!r[key]) r[key] = []
r[key].push(...el.counts)
})
return r;
}, {})
}
}
但是我没有运气让它工作。在这种情况下,我会收到此错误:
mytest3.html:10未捕获(承诺)TypeError:无法读取属性 未定义的“数据” 在myfunction(mytest3.html:10)
报告处理程序代码:
async function reportHandler(id, r2, retries) {
if (retries >= 3 || r2.error != "report_not_ready") return r2;
await sleep(2000);
var report = await getReport(id, "get");
return reportHandler(id, report, retries + 1);
}