RocketChat/Rocket.Chat · error · Meteor.Error

error-invalid-user

error-invalid-user

Error message

Invalid user

What it means

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.

Source

Thrown at apps/meteor/server/meteor-methods/rooms/addRoomModerator.ts:50

		});
	}

	const isFederated = isRoomFederated(room);

	if (!(await hasPermissionAsync(fromUserId, 'set-moderator', rid)) && !isFederated) {
		throw new Meteor.Error('error-not-allowed', 'Not allowed', {
			method: 'addRoomModerator',
		});
	}

	if (isFederated && !isFederationEnabled()) {
		throw new FederationMatrixInvalidConfigurationError('unable to change room owners');
	}

	const user = await Users.findOneById(userId);

	if (!user?.username) {
		throw new Meteor.Error('error-invalid-user', 'Invalid user', {
			method: 'addRoomModerator',
		});
	}

	const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, user._id);

	if (!subscription) {
		throw new Meteor.Error('error-user-not-in-room', 'User is not in this room', {
			method: 'addRoomModerator',
		});
	}

	if (subscription.roles && Array.isArray(subscription.roles) === true && subscription.roles.includes('moderator') === true) {
		throw new Meteor.Error('error-user-already-moderator', 'User is already a moderator', {
			method: 'addRoomModerator',
		});
	}

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Verify the target user exists and has a username before calling.
  2. Refresh the member list to drop removed users.
  3. Repair user records that are missing usernames.
Defensive patterns

Strategy: validation

Validate before calling

// confirm target user exists with a username before promoting
const res = await fetch(`/api/v1/users.info?userId=${userId}`, { headers }).then((r) => r.json());
if (!res.success || !res.user?.username) {
	throw new Error('Target user missing or has no username');
}

Type guard

function isPromotableUser(u: { _id?: string; username?: string } | null | undefined): u is { _id: string; username: string } {
	return !!u?._id && typeof u.username === 'string' && u.username.length > 0;
}

Try / catch

try {
	await Meteor.callAsync('addRoomModerator', rid, userId);
} catch (e: any) {
	if (e?.error === 'error-invalid-user') {
		// target invalid: refresh member list and reselect
	}
}

Prevention

When it happens

Trigger: 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.

Common situations: Stale member pickers; users deleted mid-session; imported accounts lacking usernames.

Related errors


AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18). Data as JSON: /api/errors/863f54197f248d46. Report an issue: GitHub.