【问题标题】:Array.push is not a function inside of Promise [closed]Array.push 不是 Promise 内部的函数 [关闭]
【发布时间】: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


【解决方案1】:

正如 Andreas 所指出的,它是一个半冒号。我猜总是检查你的分号......

【讨论】:

  • 这很有趣,因为我一直认为 ASI 规则是“如果不会导致语法错误,则在每行末尾插入一个半” - 显然这种情况发生了变化并且变得更不可预测。我总是使用分号,现在我有一个理由感谢你的问题。谢谢,OP!
  • 在执行 IIFE 之前有时需要一个分号,否则前面的 ( 将被解释为添加到前一个语句中的另一个函数调用。在设计语言时始终使用分号的另一个原因。而且,缺少分号时的错误消息通常非常无用,因为解释器正在尝试做一些你从未期望它会尝试的事情。
【解决方案2】:

使用array.save() 作为report.save() 来保存对象本身。

(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 ******
    report.save();

    (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(sub_obj) 之后插入分号可以修复它而不添加新语句。
猜你喜欢
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
  • 2017-03-28
  • 2019-07-17
  • 1970-01-01
  • 2019-02-14
  • 2014-05-07
  • 1970-01-01
相关资源
最近更新 更多