RocketChat/Rocket.Chat · error · Meteor.Error

error-invalid-user

error-invalid-user

Error message

Invalid user

What it means

setEmail throws error-invalid-user when the userId argument is falsy, before any database access. This is a caller bug: the function was invoked without a user context. It is distinct from the same code thrown later at line 65, where the id is present but no matching user document exists.

Source

Thrown at apps/meteor/server/lib/users/setEmail.ts:54

	} catch (error: any) {
		throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${error.message}`, {
			function: 'setEmail',
			message: error.message,
		});
	}
};

export const setEmail = async function (
	userId: string,
	email: string,
	shouldSendVerificationEmail = true,
	verified = false,
	updater?: Updater<IUser>,
	session?: ClientSession,
) {
	email = email.trim();
	if (!userId) {
		throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: '_setEmail' });
	}

	if (!email) {
		throw new Meteor.Error('error-invalid-email', 'Invalid email', { function: '_setEmail' });
	}

	await validateEmailDomain(email);

	const user = await Users.findOneById(userId, { session });
	if (!user) {
		throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: '_setEmail' });
	}

	// User already has desired username, return
	if (user?.emails?.[0] && user.emails[0].address === email) {
		return user;
	}

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Ensure a real authenticated user id is passed (use this.userId inside Meteor methods).
  2. Add an early guard in the caller that rejects the operation when there is no user context.
  3. Trace the call chain to find where the id becomes undefined (typos, destructuring mistakes).

Example fix

// before
await setEmail(this.userId ?? '', newEmail);

// after
if (!this.userId) {
  throw new Meteor.Error('error-invalid-user', 'Invalid user');
}
await setEmail(this.userId, newEmail);
Defensive patterns

Strategy: validation

Validate before calling

if (!userId || typeof userId !== 'string') {
  throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: 'caller' });
}
await setEmail(userId, email);

Type guard

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

Prevention

When it happens

Trigger: setEmail('', ...) or setEmail(undefined, ...) — typically from a Meteor method using this.userId while the call is unauthenticated, or a server integration that lost the id variable and passed it through.

Common situations: Server lib called during startup or from a job with no logged-in user; method invoked by an automated client without a session; a variable named differently (uid vs userId) passed as undefined; race where the user logged out mid-flow.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


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