【发布时间】:2020-08-24 02:46:57
【问题描述】:
以下代码块遇到此错误。你可以看到我在async 函数内部实例化了report = [],但在我运行async Promise.all 之前。在该承诺内部,我运行了一些处理,该处理应该获取数据并将其推回到承诺范围之外的report 数组中。我尝试从const 更改为let,并将Promise.all 放入try/catch 块中,但总是得到相同的错误...
完全错误:
TypeError: report.push(...) is not a function
(node:2568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
代码:
(async (env) => {
const report = []
const subscriptions = await xm.subscriptions.getMany(env);
await Promise.all(subscriptions.map(sub => {
let apps_and_lobs = xm.subscriptions.process_apps_and_lobs(sub.criteria.data);
let sub_obj = {
sub_name : sub.name,
sub_uuid : sub.id,
form_name : sub.form.name,
form_id : sub.form.id,
plan_name : sub.form.plan.name,
plan_id : sub.form.plan.id,
owner_targetName : sub.owner.targetName,
owner_firstName : sub.owner.firstName,
owner_lastName : sub.owner.lastName,
applications : apps_and_lobs.apps,
lob : apps_and_lobs.lobs
}
report.push(sub_obj) // <----- ***** This is failing ******
(async (env) => {
let recipients = await xm.subscriptions.getSubscribers(env, null, sub.id);
recipients.map(r => {
util.cmt(JSON.stringify(r, null, 2));
})
})(env)
}))
})(prod);
【问题讨论】:
-
您是否尝试通过在
report.push()调用上方插入debugger;语句来检查值? -
注意你的分号。在这里应该不是问题,但完全依赖 ASI 是个坏主意。
-
wtf?这是一个分号。我不敢相信这是问题所在。感谢您指出这一点...
标签: javascript arrays asynchronous promise scoping