{"record":{"id":"80ee2d247c5279fd","repo":"overleaf/overleaf","slug":"invalid-user-id-useridarg","errorCode":null,"errorMessage":"invalid --user-id: ${userIdArg}","messagePattern":"invalid --user-id: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"services/web/scripts/set_user_ai_usage.mjs","lineNumber":121,"sourceCode":"\n  # Reset a user's AI usage back to 0\n  node scripts/set_user_ai_usage.mjs \\\\\n    --user-id 5f9a2c8e1b3d4f0012abcd34 \\\\\n    --feature aiFeatureUsage --usage 0 --commit\n`\n\nif (argv.help) {\n  console.log(HELP_TEXT)\n  process.exit(0)\n}\n\nconst userIdArg = argv['user-id']\nconst edgeArg = argv.edge\nconst COMMIT = argv.commit === true\n\nif (!userIdArg) throw new Error('missing --user-id (use --help for usage)')\nif (!ObjectId.isValid(userIdArg)) {\n  throw new Error(`invalid --user-id: ${userIdArg}`)\n}\n\nlet feature\nlet usage\nif (edgeArg !== undefined) {\n  if (argv.feature !== undefined || argv.usage !== undefined) {\n    throw new Error('--edge cannot be combined with --feature or --usage')\n  }\n  const preset = EDGE_PRESETS[edgeArg]\n  if (!preset) {\n    throw new Error(\n      `invalid --edge ${JSON.stringify(edgeArg)}; expected one of ${Object.keys(EDGE_PRESETS).join(', ')}`\n    )\n  }\n  feature = preset.feature\n  usage = preset.usage\n} else {\n  feature = argv.feature","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/overleaf/overleaf/blob/28ad3b03b71cb4311decdcb55c36b33ec10d72db/services/web/scripts/set_user_ai_usage.mjs#L103-L139","documentation":"Immediately after checking presence of --user-id, set_user_ai_usage.mjs validates it with ObjectId.isValid and throws this error for syntactically invalid ids. This surfaces a clear message instead of letting the MongoDB driver fail later with a BSON casting error.","triggerScenarios":"Passing --user-id an email address, username, numeric/SQL id, UUID, or a hex string that is not exactly 24 valid hex characters (too short/long, non-hex characters).","commonSituations":"Email pasted in place of the Mongo _id; id truncated by line-wrap when copying from a ticket; id from another environment with different format; quotes/whitespace included in the pasted value.","solutions":["Supply a 24-char hex ObjectId: node scripts/set_user_ai_usage.mjs --user-id 660fabc123def45678901234 ...","Resolve the _id from email: db.users.findOne({email: '<email>'}, {_id: 1})","Sanitize the value (strip whitespace/quotes) and re-check length (24) and hex charset","Run ObjectId.isValid('<value>') locally to confirm before invoking the script"],"exampleFix":"// before\nnode scripts/set_user_ai_usage.mjs --user-id alice@corp.com --feature chat --usage 5\n// after\nnode scripts/set_user_ai_usage.mjs --user-id 660fabc123def45678901234 --feature chat --usage 5","handlingStrategy":"validation","validationCode":"import { ObjectId } from 'mongodb'\nconst id = argv['user-id']\nif (!id || !ObjectId.isValid(String(id))) {\n  throw new Error(`--user-id must be a 24-char hex ObjectId, got: ${JSON.stringify(id)}`)\n}","typeGuard":"function isValidObjectIdString(v) { return typeof v === 'string' && /^[0-9a-fA-F]{24}$/.test(v) }","tryCatchPattern":"try {\n  await runScript(['--user-id', id, ...rest])\n} catch (err) {\n  if (/invalid --user-id/.test(err.message)) {\n    console.error(`'${id}' is not an ObjectId — resolve _id via db.users.findOne({email: ...})`)\n  } else throw err\n}","preventionTips":["Pass the 24-hex Mongo _id, never an email or external id","Trim/strip quotes from pasted values","Pre-validate with ObjectId.isValid in wrappers","Keep ids in copy-safe formats in documentation"],"tags":["mongodb","validation","objectid","cli"],"backgroundTag":"invalid-objectid","analyzedSha":"28ad3b03b71cb4311decdcb55c36b33ec10d72db","analyzedAt":"2026-09-03T02:10:22.807Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T07:17:11.731Z"}