overleaf/overleaf · error · Error
invalid --user-id: ${userIdArg}
Error message
invalid --user-id: ${userIdArg} What it means
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.
Source
Thrown at services/web/scripts/set_user_ai_usage.mjs:121
# Reset a user's AI usage back to 0
node scripts/set_user_ai_usage.mjs \\
--user-id 5f9a2c8e1b3d4f0012abcd34 \\
--feature aiFeatureUsage --usage 0 --commit
`
if (argv.help) {
console.log(HELP_TEXT)
process.exit(0)
}
const userIdArg = argv['user-id']
const edgeArg = argv.edge
const COMMIT = argv.commit === true
if (!userIdArg) throw new Error('missing --user-id (use --help for usage)')
if (!ObjectId.isValid(userIdArg)) {
throw new Error(`invalid --user-id: ${userIdArg}`)
}
let feature
let usage
if (edgeArg !== undefined) {
if (argv.feature !== undefined || argv.usage !== undefined) {
throw new Error('--edge cannot be combined with --feature or --usage')
}
const preset = EDGE_PRESETS[edgeArg]
if (!preset) {
throw new Error(
`invalid --edge ${JSON.stringify(edgeArg)}; expected one of ${Object.keys(EDGE_PRESETS).join(', ')}`
)
}
feature = preset.feature
usage = preset.usage
} else {
feature = argv.featureView on GitHub (pinned to 28ad3b03b7)
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
Example fix
// before node scripts/set_user_ai_usage.mjs --user-id alice@corp.com --feature chat --usage 5 // after node scripts/set_user_ai_usage.mjs --user-id 660fabc123def45678901234 --feature chat --usage 5
Defensive patterns
Strategy: validation
Validate before calling
import { ObjectId } from 'mongodb'
const id = argv['user-id']
if (!id || !ObjectId.isValid(String(id))) {
throw new Error(`--user-id must be a 24-char hex ObjectId, got: ${JSON.stringify(id)}`)
} Type guard
function isValidObjectIdString(v) { return typeof v === 'string' && /^[0-9a-fA-F]{24}$/.test(v) } Try / catch
try {
await runScript(['--user-id', id, ...rest])
} catch (err) {
if (/invalid --user-id/.test(err.message)) {
console.error(`'${id}' is not an ObjectId — resolve _id via db.users.findOne({email: ...})`)
} else throw err
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- user ID not valid
- bad docId: usage: $ node scripts/remove_deleted_docs.js [DOC
- invalid project id
- user ID not valid: ${userId}
- users is not an array
AI-assisted analysis of overleaf/overleaf@28ad3b03b7 (2026-09-03).
Data as JSON: /api/errors/80ee2d247c5279fd.
Report an issue: GitHub.