javascript - Array.push 不是 Promise 内部的函数

标签 javascript arrays asynchronous promise scoping

以下代码块遇到此错误。你可以看到我实例化了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/

相关文章:

javascript - 仅在 IndexedDB 中更新对象后运行代码(特别是在 Chrome 中)

python - 将 asyncio 与多处理结合起来会出现什么样的问题(如果有的话)?

python - 在 Python 网络应用程序中处理较大文件上传的最具可扩展性的方法是什么?

javascript - 处理在 Canvas 上单独绘制的矩形实例的最佳方法

javascript - Object.create() 使用

javascript - 如何将数据从Java方法发送到javascript函数?

php - @$array ['possibly_missing_key' ] 是反模式吗?

javascript - 将 Postgres Heroku 与 Knex 连接不工作

以数字作为属性名称的 Javascript 数组

javascript - 从数组中删除字符串