RocketChat/Rocket.Chat · error · MeteorError

error-invalid-user

error-invalid-user

Error message

Invalid user

What it means

Thrown by LivechatEnterprise.addMonitor when no user is found for the given username (Users.findOneByUsername returns null). MeteorError code 'error-invalid-user', method 'livechat:addMonitor'. This is the user-existence guard before the addMonitor role assignment (error 238).

Source

Thrown at apps/meteor/ee/server/lib/omnichannel/LivechatEnterprise.ts:21

import { Users, OmnichannelServiceLevelAgreements, LivechatTag, LivechatUnitMonitors, LivechatUnit } from '@rocket.chat/models';
import { getUnitsFromUser } from '@rocket.chat/omni-core-ee';
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { updateSLAInquiries } from './Helper';
import { removeSLAFromRooms } from './SlaHelper';
import { callbacks } from '../../../../server/lib/callbacks';
import { addUserRolesAsync } from '../../../../server/lib/roles/addUserRoles';
import { removeUserFromRolesAsync } from '../../../../server/lib/roles/removeUserFromRoles';

export const LivechatEnterprise = {
	async addMonitor(username: string) {
		const user = await Users.findOneByUsername<Pick<IUser, '_id' | 'username' | 'roles'>>(username, {
			projection: { _id: 1, username: 1, roles: 1 },
		});

		if (!user) {
			throw new MeteorError('error-invalid-user', 'Invalid user', {
				method: 'livechat:addMonitor',
			});
		}

		if (!(await addUserRolesAsync(user._id, ['livechat-monitor']))) {
			throw new MeteorError('error-adding-monitor-role', 'Error adding monitor role', {
				method: 'livechat:addMonitor',
			});
		}

		return user;
	},

	async removeMonitor(username: string) {
		const user = await Users.findOneByUsername<Pick<IUser, '_id'>>(username, {
			projection: { _id: 1 },
		});

View on GitHub (pinned to f9d3ec372b)

Solutions

  1. Confirm the username exists (case-sensitive lookup) before calling addMonitor.
  2. Resolve/normalize the username from a user picker rather than free text.
  3. If the user was deleted, restore or pick a different monitor.

Example fix

// before
await LivechatEnterprise.addMonitor('jdoe'); // throws if missing

// after
const u = await Users.findOneByUsername('jdoe');
if (!u) throw new Meteor.Error('error-invalid-user', 'Pick a valid user');
await LivechatEnterprise.addMonitor(u.username);
Defensive patterns

Strategy: validation

Validate before calling

async function findMonitorUser(username: string) {
  const u = await Users.findOneByUsername(username, { projection: { _id: 1, username: 1, roles: 1 } });
  if (!u) throw new Meteor.Error('error-invalid-user', 'User not found', { method: 'livechat:addMonitor' });
  return u;
}

Type guard

const isUsername = (s: unknown): s is string => typeof s === 'string' && s.trim().length > 0;

Try / catch

try { await LivechatEnterprise.addMonitor(username); } catch (e) {
  if (e?.error === 'error-invalid-user') { /* repick from user list */ } else throw e;
}

Prevention

When it happens

Trigger: Calling livechat:addMonitor with a username that does not exist; typo in username; user was deleted between the admin UI lookup and the API call.

Common situations: Admin types a non-existent username in the add-monitor dialog; import script passes an unnormalized username (case/whitespace); user deactivated/deleted.

Related errors


AI-assisted analysis of RocketChat/Rocket.Chat@f9d3ec372b (2026-08-12). Data as JSON: /api/errors/1f2cd73f6272996a. Report an issue: GitHub.