{"record":{"id":"499dbee3d3b7daba","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-499dbe","errorCode":"error-invalid-user","errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/users/setEmail.ts","lineNumber":54,"sourceCode":"\t} catch (error: any) {\n\t\tthrow new Meteor.Error('error-email-send-failed', `Error trying to send email: ${error.message}`, {\n\t\t\tfunction: 'setEmail',\n\t\t\tmessage: error.message,\n\t\t});\n\t}\n};\n\nexport const setEmail = async function (\n\tuserId: string,\n\temail: string,\n\tshouldSendVerificationEmail = true,\n\tverified = false,\n\tupdater?: Updater<IUser>,\n\tsession?: ClientSession,\n) {\n\temail = email.trim();\n\tif (!userId) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', { function: '_setEmail' });\n\t}\n\n\tif (!email) {\n\t\tthrow new Meteor.Error('error-invalid-email', 'Invalid email', { function: '_setEmail' });\n\t}\n\n\tawait validateEmailDomain(email);\n\n\tconst user = await Users.findOneById(userId, { session });\n\tif (!user) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', { function: '_setEmail' });\n\t}\n\n\t// User already has desired username, return\n\tif (user?.emails?.[0] && user.emails[0].address === email) {\n\t\treturn user;\n\t}\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/users/setEmail.ts#L36-L72","documentation":"setEmail throws error-invalid-user when the userId argument is falsy, before any database access. This is a caller bug: the function was invoked without a user context. It is distinct from the same code thrown later at line 65, where the id is present but no matching user document exists.","triggerScenarios":"setEmail('', ...) or setEmail(undefined, ...) — typically from a Meteor method using this.userId while the call is unauthenticated, or a server integration that lost the id variable and passed it through.","commonSituations":"Server lib called during startup or from a job with no logged-in user; method invoked by an automated client without a session; a variable named differently (uid vs userId) passed as undefined; race where the user logged out mid-flow.","solutions":["Ensure a real authenticated user id is passed (use this.userId inside Meteor methods).","Add an early guard in the caller that rejects the operation when there is no user context.","Trace the call chain to find where the id becomes undefined (typos, destructuring mistakes)."],"exampleFix":"// before\nawait setEmail(this.userId ?? '', newEmail);\n\n// after\nif (!this.userId) {\n  throw new Meteor.Error('error-invalid-user', 'Invalid user');\n}\nawait setEmail(this.userId, newEmail);","handlingStrategy":"validation","validationCode":"if (!userId || typeof userId !== 'string') {\n  throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: 'caller' });\n}\nawait setEmail(userId, email);","typeGuard":"const hasUserId = (id: unknown): id is string => typeof id === 'string' && id.trim().length > 0;","tryCatchPattern":null,"preventionTips":["Always derive the user id from the authenticated context (this.userId / uid) rather than client input.","Fail fast in method entry points when there is no user context.","Use TypeScript strict null checks so undefined ids surface at compile time."],"tags":["validation","user-management","email"],"backgroundTag":"missing-required-parameter","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}