{"record":{"id":"9a1d0194ecda006a","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-room-9a1d01","errorCode":"error-invalid-room","errorMessage":"Invalid room","messagePattern":"Invalid room","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/rooms/addUsersToRoom.ts","lineNumber":38,"sourceCode":"\nexport const sanitizeUsername = (username: string) => {\n\tconst isFederatedUsername = username.includes('@') && username.includes(':');\n\tif (isFederatedUsername) {\n\t\treturn username;\n\t}\n\n\treturn username.replace(/(^@)|( @)/, '');\n};\n\nexport const addUsersToRoomMethod = async (userId: string, data: { rid: string; users: string[] }, user?: IUser): Promise<boolean> => {\n\tif (!userId) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', {\n\t\t\tmethod: 'addUsersToRoom',\n\t\t});\n\t}\n\n\tif (!Match.test(data.rid, String)) {\n\t\tthrow new Meteor.Error('error-invalid-room', 'Invalid room', {\n\t\t\tmethod: 'addUsersToRoom',\n\t\t});\n\t}\n\n\t// Get user and room details\n\tconst room = await Rooms.findOneById(data.rid);\n\tif (!room) {\n\t\tthrow new Meteor.Error('error-invalid-room', 'Invalid room', {\n\t\t\tmethod: 'addUsersToRoom',\n\t\t});\n\t}\n\n\tconst subscription = await Subscriptions.findOneByRoomIdAndUserId(data.rid, userId, {\n\t\tprojection: { _id: 1 },\n\t});\n\tconst userInRoom = subscription != null;\n\n\tif (room.t === 'd' && !isRoomNativeFederated(room)) {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/rooms/addUsersToRoom.ts#L20-L56","documentation":"Thrown by addUsersToRoomMethod() in apps/meteor/server/meteor-methods/rooms/addUsersToRoom.ts:38 when Match.test(data.rid, String) fails — data.rid is not a string (number, undefined, null, object). This is a type-shape check only; it fires before any database lookup, so the room may or may not exist.","triggerScenarios":"Calling addUsersToRoomMethod with data.rid undefined (property misspelled or missing), a numeric ID coerced by JSON inputs, an object (e.g. passing the whole room document), or null; API/migration shims that map fields incorrectly.","commonSituations":"Schema drift between client payloads and the method signature; refactors renaming rid to roomId without updating all callers; JSON sources where the ID arrives as a Mongo ObjectId object rather than its string form.","solutions":["Ensure data.rid is a string before calling: if (typeof data.rid !== 'string') reject.","Stringify ObjectIds at the boundary: String(room._id) or room._id.valueOf() when bridging from driver objects.","Add a runtime schema check on the payload (e.g. Match.test(data, Match.ObjectIncluding({ rid: String, users: [String] }))) at your own entry points.","For REST integrations, use POST /v1/channels.invite / POST /v1/groups.invite where room is identified by roomId and validated by the API layer."],"exampleFix":"// before\nawait addUsersToRoomMethod(uid, { rid: payload.roomId?.toHexString?.() ?? payload.roomId, users });\n\n// after\nconst rid = String(payload.roomId ?? '');\nif (!rid) throw new Error('rid is required and must be a string');\nawait addUsersToRoomMethod(uid, { rid, users });","handlingStrategy":"type-guard","validationCode":"if (!Match.test(data.rid, String)) {\n  throw new Error('rid must be a string');\n}","typeGuard":"const isRoomIdString = (v: unknown): v is string => typeof v === 'string' && v.length > 0;","tryCatchPattern":"try {\n  await addUsersToRoomMethod(uid, data);\n} catch (e) {\n  if (e instanceof Meteor.Error && e.error === 'error-invalid-room' && typeof data.rid !== 'string') {\n    // shape error: normalize rid to string before retry\n  }\n}","preventionTips":["Validate payload shape at the boundary with Match/Object schemas.","Stringify Mongo ObjectIds once, at the edge: String(doc._id).","Keep TypeScript types for payloads so renames/type drift fail at compile time."],"tags":["meteor-method","validation","match","type-error","rooms"],"backgroundTag":"invalid-argument-type","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}