{"record":{"id":"d05be806bddab8d4","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-d05be8","errorCode":"error-invalid-user","errorMessage":"There is no user with this username","messagePattern":"There is no user with this username","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/api/v1/roles.ts","lineNumber":287,"sourceCode":"\t\t\t\t}),\n\t\t\t\t400: validateBadRequestErrorResponse,\n\t\t\t\t401: validateUnauthorizedErrorResponse,\n\t\t\t\t403: validateForbiddenErrorResponse,\n\t\t\t},\n\t\t},\n\t\tasync function action() {\n\t\t\tconst { bodyParams } = this;\n\n\t\t\tconst { roleId, username, scope } = bodyParams;\n\n\t\t\tif (!roleId) {\n\t\t\t\treturn API.v1.failure('error-invalid-role-properties');\n\t\t\t}\n\n\t\t\tconst user = await Users.findOneByUsername(username);\n\n\t\t\tif (!user) {\n\t\t\t\tthrow new Meteor.Error('error-invalid-user', 'There is no user with this username');\n\t\t\t}\n\n\t\t\tconst role = await Roles.findOneById(roleId);\n\n\t\t\tif (!role) {\n\t\t\t\tthrow new Meteor.Error('error-invalid-roleId', 'This role does not exist');\n\t\t\t}\n\n\t\t\tif (!(await hasAnyRoleAsync(user._id, [role._id], scope))) {\n\t\t\t\tthrow new Meteor.Error('error-user-not-in-role', 'User is not in this role');\n\t\t\t}\n\n\t\t\tif (role._id === 'admin') {\n\t\t\t\tconst adminCount = await Roles.countUsersInRole('admin');\n\t\t\t\tif (adminCount === 1) {\n\t\t\t\t\tthrow new Meteor.Error('error-admin-required', 'You need to have at least one admin');\n\t\t\t\t}\n\t\t\t}","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/server/api/v1/roles.ts#L269-L305","documentation":"Thrown by POST roles.removeUserFromRole when Users.findOneByUsername(username) returns null. The endpoint requires 'access-permissions' and the username came from the validated body. Returns a structured Meteor.Error('error-invalid-user', 'There is no user with this username').","triggerScenarios":"POST /api/v1/roles.removeUserFromRole with a username that does not exist (typo, renamed/deactivated user, username vs user id confusion).","commonSituations":"Client sends the user _id or email instead of the username; the target user was renamed or deactivated and the username no longer resolves; cross-workspace username.","solutions":["Resolve the username from the users endpoint (e.g. users.info) before calling removeUserFromRole.","Ensure you pass username (not _id, not email) as defined by isRoleRemoveUserFromRoleProps.","Refresh cached usernames if the target may have been renamed."],"exampleFix":"// before\nawait fetch('/api/v1/roles.removeUserFromRole', { method:'POST', body: JSON.stringify({ roleId, username: userId }) }); // wrong field\n\n// after\nawait fetch('/api/v1/roles.removeUserFromRole', { method:'POST', body: JSON.stringify({ roleId, username }) });","handlingStrategy":"validation","validationCode":"// Resolve a real username before removing from a role\nconst { user } = await fetch(`/api/v1/users.info?username=${encodeURIComponent(username)}`).then(r => r.json());\nif (!user) throw new Error(`no user with username ${username}`);\nawait fetch('/api/v1/roles.removeUserFromRole', { method:'POST', body: JSON.stringify({ roleId, username: user.username }) });","typeGuard":"function isUsername(value: unknown): value is string {\n  return typeof value === 'string' && value.trim().length > 0 && !value.includes('@') && !/^[a-z0-9]{17}$/i.test(value);\n}","tryCatchPattern":null,"preventionTips":["Pass username, not _id or email.","Resolve usernames freshly if users may have been renamed."],"tags":["roles","rest-api","not-found","user","validation"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}