2

Running into this error for the below code block. You can see that I instantiate report = [] inside of an async function but before I run an async Promise.all. Inside of that promise, I run some processing which should take data and push it back into the report array which is scoped outside of the promise. I have tried changing to let from const as well as putting the Promise.all into a try/catch block but always get the same error...

Full Error:

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)

Code:

(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);
vin_Bin87
  • 318
  • 8
  • 18

2 Answers2

2

As pointed out by Andreas, it was a semi colon. Always check your semi-colons I guess...

vin_Bin87
  • 318
  • 8
  • 18
  • 1
    That's interesting because I've always thought ASI rules were "insert a semi at the end of every line if it doesn't cause a syntax error" - apparently this changed and has become less predictable. I always use semicolons, and now I have a reason thanks to your question. Thanks, OP! – amphetamachine May 08 '20 at 14:17
  • 2
    @amphetamachine [What are the rules for JavaScript's automatic semicolon insertion (ASI)?](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) – Andreas May 08 '20 at 14:43
  • You sometimes need a semi-colon BEFORE you do an IIFE or the leading `(` will get interpreted as another function call added onto the previous statement. Just another reason to always use semi-colons as the language was designed. And, the error messages when you're missing a semi-colon are usually very unhelpful because the interpreter is trying to do something you would never expect it to try. – jfriend00 May 08 '20 at 14:53
1

use array.save() as report.save() to save object itself.

(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);
Pushpendra Kumar
  • 1,721
  • 1
  • 15
  • 21