RocketChat/Rocket.Chat · error · Error

Error creating/updating unit

Error message

Error creating/updating unit

What it means

Thrown by LivechatUnitRaw.createOrUpdateUnit after either an insert or an update when _id is still falsy. Because updateOne does not return a new id and insertOne is expected to populate insertedId, a falsy _id here means the MongoDB write did not produce an identifier — an exceptional, low-level persistence failure. It is a plain Error (not a Meteor.Error) since it is raised inside a model/raw layer.

Source

Thrown at apps/meteor/ee/server/models/raw/LivechatUnit.ts:77

		monitors = ([] as { monitorId: string; username: string }[]).concat(monitors || []);
		ancestors = ([] as string[]).concat(ancestors || []);

		const record = {
			name,
			visibility,
			type: 'u',
			numMonitors: monitors.length,
			numDepartments: departments.length,
		};

		if (_id) {
			await this.updateOne({ _id }, { $set: record });
		} else {
			_id = (await this.insertOne(record)).insertedId;
		}

		if (!_id) {
			throw new Error('Error creating/updating unit');
		}

		ancestors.splice(0, 0, _id);

		const savedMonitors = (await LivechatUnitMonitors.findByUnitId(_id).toArray()).map(({ monitorId }) => monitorId);
		const monitorsToSave = monitors.map(({ monitorId }) => monitorId);

		// remove other monitors
		for await (const monitorId of savedMonitors) {
			if (!monitorsToSave.includes(monitorId)) {
				await LivechatUnitMonitors.removeByUnitIdAndMonitorId(_id, monitorId);
			}
		}

		for await (const monitor of monitors) {
			await LivechatUnitMonitors.saveMonitor({
				monitorId: monitor.monitorId,
				unitId: _id,

View on GitHub (pinned to f9d3ec372b)

Solutions

  1. Check MongoDB server logs and the Rocket.Chat mongo connection for the failing write.
  2. Ensure createOrUpdateUnit is called with _id === null (not '' or undefined) for creates and a real ObjectId/string for updates.
  3. Confirm the livechat_department collection is a real collection, not a view, and has no conflicting JSON Schema validator.
  4. Retry the unit create/update after confirming connectivity; if it persists, inspect the WriteError/duplicate key.

Example fix

// before
await LivechatUnit.createOrUpdateUnit('', { name, visibility }, ancestors, monitors, departments);

// after
await LivechatUnit.createOrUpdateUnit(null, { name, visibility }, ancestors, monitors, departments);
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await LivechatUnit.createOrUpdateUnit(unitId, { name, visibility }, ancestors, monitors, departments);
} catch (e) {
  if (e.message === 'Error creating/updating unit') {
    // inspect Mongo connection / server logs, then retry once with a clean session
  } else throw e;
}

Prevention

When it happens

Trigger: insertOne resolves but insertedId is undefined (driver bug, interrupted connection, or the collection is a view/invalid); updateOne was called with an _id that did not match any document and the code path kept _id as the original null; MongoDB reject due to duplicate key or write-concern timeout that did not throw earlier.

Common situations: Network blip / replica-set failover between the write and the driver ack; passing _id as an empty string instead of null when you want a create; sharded cluster where the insert returned no insertedId; schema/validation rule on the livechat_department collection that silently rejects the document.

Related errors


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