{"record":{"id":"5bf8135809165cb4","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-5bf813","errorCode":"error-invalid-user","errorMessage":"The optional \"userId\" param provided does not match any users","messagePattern":"The optional \"userId\" param provided does not match any users","errorType":"exception","errorClass":"Meteor.Error","httpStatus":400,"severity":"error","filePath":"apps/meteor/server/api/v1/users.ts","lineNumber":250,"sourceCode":"\t\t\tauthRequired: true,\n\t\t\tbody: isUsersSetPreferencesParamsPOST,\n\t\t\tresponse: {\n\t\t\t\t200: userObjectResponse,\n\t\t\t\t400: validateBadRequestErrorResponse,\n\t\t\t\t401: validateUnauthorizedErrorResponse,\n\t\t\t},\n\t\t},\n\t\tasync function action() {\n\t\t\tif (\n\t\t\t\tthis.bodyParams.userId &&\n\t\t\t\tthis.bodyParams.userId !== this.userId &&\n\t\t\t\t!(await hasPermissionAsync(this.user, 'edit-other-user-info'))\n\t\t\t) {\n\t\t\t\tthrow new Meteor.Error('error-action-not-allowed', 'Editing user is not allowed');\n\t\t\t}\n\t\t\tconst userId = this.bodyParams.userId ? this.bodyParams.userId : this.userId;\n\t\t\tif (!(await Users.findOneById(userId))) {\n\t\t\t\tthrow new Meteor.Error('error-invalid-user', 'The optional \"userId\" param provided does not match any users');\n\t\t\t}\n\n\t\t\tconst { statusVisibilityDenied: _ownBlockList, ...preferences } = this.bodyParams.data;\n\n\t\t\tawait saveUserPreferences(userId === this.userId ? this.bodyParams.data : preferences, userId);\n\t\t\tconst user = await Users.findOneById(userId, {\n\t\t\t\tprojection: {\n\t\t\t\t\t'settings.preferences': 1,\n\t\t\t\t\t'language': 1,\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tif (!user) {\n\t\t\t\treturn API.v1.failure('User not found');\n\t\t\t}\n\n\t\t\tconst { statusVisibilityDenied, ...savedPreferences } = user.settings?.preferences ?? {};\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/e4b8178b205510181a96ceefee043d0abcd13e5a/apps/meteor/server/api/v1/users.ts#L232-L268","documentation":"Thrown by POST users.setPreferences when the resolved target userId matches no user document. After the permission check, the endpoint does a Users.findOneById on the passed userId (or the caller's own id) and requires a hit before saving preferences.","triggerScenarios":"POST with {userId: 'nonExistentOrDeletedId', ...}; userId with a stray space or wrong casing/copy-paste artifact; the caller's own account deleted between token issuance and the call (token still resolves to a dead user id).","commonSituations":"Stale user references in client state after account deletion; tools importing user lists where an id column is off by one character; race with account deletion during an active session.","solutions":["Verify the id first with GET users.info?userId=... — if that 404s/fails, fix the reference","For self-edits, omit userId so the server uses the authenticated id","Refresh your user cache when this fires rather than retrying the same id"],"exampleFix":"// before\nawait sdk.post('users.setPreferences', { userId, data }); // userId from stale cache\n// after\nconst info = await sdk.get('users.info', { userId });\nif (!info.user) throw new Error('unknown target user');\nawait sdk.post('users.setPreferences', { userId, data });","handlingStrategy":"validation","validationCode":"if (targetUserId) {\n  const { users } = await sdk.get('users.list', {}).catch(() => ({ users: [] }));\n  if (targetUserId !== myUserId && !users.some((u) => u._id === targetUserId)) throw new Error('target user does not exist');\n}","typeGuard":null,"tryCatchPattern":"catch (e) { if (e?.error === 'error-invalid-user') refreshUserReferences(); else throw e; }","preventionTips":["Verify user ids with users.info before mutating them","Drop stale user references from client caches on this error"],"tags":["rest-api","users","not-found","preferences","validation"],"backgroundTag":"user-not-found","analyzedSha":"e4b8178b205510181a96ceefee043d0abcd13e5a","analyzedAt":"2026-08-21T15:01:34.830Z","contentChangedAt":"2026-08-21T15:01:34.830Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}