Unhandled rejection Error: A required argument is missing

so I was using this file called revertranks.js and I tried to run it with node revertranks.js.

When I run it, there would be this error saying the following:

“Unhandled rejection Error: A required argument is missing”

I was wondering why this was happening so I was checking this website and tried other solutions, however, none worked. One which made the error go away actually just made the code stop.

Here is the code:

 * About:
 * Reverts all rank change actions of a group after a specified date, with the option to filter by user.
 */

// Settings
const cookie = process.env.Cookie || '' // Roblox account .ROBLOSECURITY cookie
const options = {
  group: (process.env.GroupId), // Group ID
  userId: (process.env.AA1), // Revert rank changes made by this specified user - NOTE: When <= 0 any rank changes will be reverted
  afterDate: new Date('2021-01-01 00:00 CDT') // Date after which any rank changes will be reverted
}

// Dependencies
const rbx = require('noblox.js')
const logUpdate = require('log-update')

// Main
let scanning = true
const logItems = {
  scanned: 0,
  filtered: 0,
  reverted: 0,
  failed: 0
}

async function getAuditLogPage (getAuditLogOptions, cursor) {
  getAuditLogOptions.cursor = cursor || ''

  const auditLogPage = await rbx.getAuditLog(getAuditLogOptions)

  return auditLogPage
}

function filterAuditLogItems (auditLogItems) {
  const filteredAuditLogItems = []

  for (const auditLogItem of auditLogItems) {
    if (Date.parse(auditLogItem.created) > options.afterDate) {
      logItems.filtered++
      filteredAuditLogItems.push(auditLogItem.description)
    }
  }

  return filteredAuditLogItems
}

async function revertAuditLogItems (auditLogItems) {
  for (const auditLogItem of auditLogItems) {
    const setRankOptions = {
      group: options.group,
      target: auditLogItem.TargetId,
      roleset: auditLogItem.OldRoleSetId
    }

    await rbx.setRank(setRankOptions)
      .then(() => {
        logItems.reverted++
      })
      .catch((e) => {
        logItems.failed++
      })
  }
}

rbx.setCookie(process.env.Cookie)
  .then(async () => {
    console.time('Time taken')

    const logUpdater = setInterval(() => {
      logUpdate(`Scanned: ${logItems.scanned}\nFiltered: ${logItems.filtered}\nReverted: ${logItems.reverted}\nFailed: ${logItems.failed}`)

      if (!scanning && logItems.reverted + logItems.failed === logItems.filtered) {
        clearInterval(logUpdater)

        console.timeEnd('Time taken')
      }
    }, 100)

    const getAuditLogOptions = {
      group: options.group,
      actionType: 'changeRank',
      userId: options.userId > 0 ? options.userId : null,
      limit: 100
    }

    let auditLogPage = await getAuditLogPage(getAuditLogOptions)
    logItems.scanned += auditLogPage.data.length

    await revertAuditLogItems(filterAuditLogItems(auditLogPage.data))

    while (auditLogPage.nextPageCursor !== null && Date.parse(auditLogPage.data[auditLogPage.data.length - 1].created) > options.afterDate) {
      auditLogPage = await getAuditLogPage(getAuditLogOptions, auditLogPage.nextPageCursor)
      logItems.scanned += auditLogPage.data.length

      await revertAuditLogItems(filterAuditLogItems(auditLogPage.data))
    }

    scanning = false
  
  })

Here is the error message: Imgur: The magic of the Internet

Hi @hmok - does the unhandled error come up after every interval? I suggest adding .catch calls to each of the await calls there so you can see exactly where the error is occurring. I think one of your function calls needs a certain number of arguments and is not getting them for some reason.

I added catch calls, however, it still shows the same error.

Do the catch calls have console loggin inside them and if so are they not being triggered? Can you share a new snippet of that file?

1 Like

I just got this new error…

They are and I just fixed that, and there came the new error.

Okay, so this is saying that a revertAuditLogItems call is failing and the failure isn’t being handled/caught in the promise it’s occurring. So you should make sure every promise has a catch, and you can try logging out whatever arguments are given to revertAuditLogItems to see what is being passed each time it’s called.

1 Like

Good news!

It scanned everything now, however, it won’t actually revert or fail, this might be the same error, however, I added a catch to every promise now.

Awesome, so yeah you have the same error, it’s just because handled better. Now I suggest you try to make sure ever call of revertAuditLogItems is included the arguments it needs - often I do this by just adding a console log to the first line of the function and print out whatever argument the function should have, so I can see if that’s the argument that is missing. It sounds like you’re on the right path!

1 Like

Can you rephrase that? Sorry, not getting what you’re saying.

Just understood…

Why should I log all of this?

I know the issue!

The function works, however, one part of the function doesn’t actually work.

    await rbx.setRank(setRankOptions)
      .then(() => {
        logItems.reverted++
      console.log("reverted")
      })
      .catch((e) => {
        logItems.failed++
      console.log("failed")
      });

It logs set rank / revert, however, fails to do setRank, you can also see that from the error.

I don’t know how to really fix this though.

I GOT IT TO WORK! I fixed a few lines and it works! Thank you so much for your help.

4 Likes

Awesome, great to hear and glad I could help!

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.