{"record":{"id":"863f54197f248d46","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-863f54","errorCode":"error-invalid-user","errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/rooms/addRoomModerator.ts","lineNumber":50,"sourceCode":"\t\t});\n\t}\n\n\tconst isFederated = isRoomFederated(room);\n\n\tif (!(await hasPermissionAsync(fromUserId, 'set-moderator', rid)) && !isFederated) {\n\t\tthrow new Meteor.Error('error-not-allowed', 'Not allowed', {\n\t\t\tmethod: 'addRoomModerator',\n\t\t});\n\t}\n\n\tif (isFederated && !isFederationEnabled()) {\n\t\tthrow new FederationMatrixInvalidConfigurationError('unable to change room owners');\n\t}\n\n\tconst user = await Users.findOneById(userId);\n\n\tif (!user?.username) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', {\n\t\t\tmethod: 'addRoomModerator',\n\t\t});\n\t}\n\n\tconst subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, user._id);\n\n\tif (!subscription) {\n\t\tthrow new Meteor.Error('error-user-not-in-room', 'User is not in this room', {\n\t\t\tmethod: 'addRoomModerator',\n\t\t});\n\t}\n\n\tif (subscription.roles && Array.isArray(subscription.roles) === true && subscription.roles.includes('moderator') === true) {\n\t\tthrow new Meteor.Error('error-user-already-moderator', 'User is already a moderator', {\n\t\t\tmethod: 'addRoomModerator',\n\t\t});\n\t}\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/rooms/addRoomModerator.ts#L32-L68","documentation":"In addRoomModerator, after room and authorization checks pass, Users.findOneById(userId) returning null or a document without username throws error-invalid-user. As with the leader variant, room roles require a username because the subscription lookup and system message depend on it.","triggerScenarios":"Promoting a deleted or nonexistent userId; targeting an app/bot user without a username; the user was removed between the member list rendering and the call.","commonSituations":"Stale member pickers; users deleted mid-session; imported accounts lacking usernames.","solutions":["Verify the target user exists and has a username before calling.","Refresh the member list to drop removed users.","Repair user records that are missing usernames."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// confirm target user exists with a username before promoting\nconst res = await fetch(`/api/v1/users.info?userId=${userId}`, { headers }).then((r) => r.json());\nif (!res.success || !res.user?.username) {\n\tthrow new Error('Target user missing or has no username');\n}","typeGuard":"function isPromotableUser(u: { _id?: string; username?: string } | null | undefined): u is { _id: string; username: string } {\n\treturn !!u?._id && typeof u.username === 'string' && u.username.length > 0;\n}","tryCatchPattern":"try {\n\tawait Meteor.callAsync('addRoomModerator', rid, userId);\n} catch (e: any) {\n\tif (e?.error === 'error-invalid-user') {\n\t\t// target invalid: refresh member list and reselect\n\t}\n}","preventionTips":["Refresh member lists to exclude deleted users.","Filter out accounts without usernames in role-management UIs.","Validate userId shape before calling."],"tags":["meteor-methods","users","not-found","roles"],"backgroundTag":"user-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}