以下代码块遇到此错误。你可以看到我实例化了report = []
async
的内部函数但在我运行 async Promise.all
之前。在这个 promise 中,我运行了一些处理,该处理应该获取数据并将其推回 report
范围超出 promise 范围的数组。我尝试更改为 let
来自const
以及输入 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);
最佳答案
正如 Andreas 所指出的,它是一个分号。我猜一定要检查你的分号...
关于javascript - Array.push 不是 Promise 内部的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61680943/