pubkey/rxdb · error · RxError

DOC2

DOC2

Error message

RxDB Error-Code: DOC2. Hint: Error messages are not included in RxDB core to reduce build size. To show the full error messages and to ensure that you do not make any mistakes when using RxDB, use the dev-mode plugin when you are in development mode: https://rxdb.info/dev-mode.html?console=error

What it means

DOC2 is thrown when you try to observe the primary key field of a document via RxDocument.get$(path), e.g. get$('id'). The primary key never changes for a document, so observing it is pointless; read it directly with doc.primary or doc.get('id')... actually read it statically. This check only runs in dev mode.

Source

Thrown at src/rx-document.ts:145

            _this.$,
            _this.getLatest()._data,
            _this.collection.database
        );
    },

    /**
     * returns observable of the value of the given path
     */
    get$(this: RxDocument, path: string): Observable<any> {
        if (overwritable.isDevMode()) {
            if (path.includes('.item.')) {
                throw newRxError('DOC1', {
                    path
                });
            }

            if (path === this.primaryPath) {
                throw newRxError('DOC2');
            }

            // final fields cannot be modified and so also not observed
            if (this.collection.schema.finalFields.includes(path)) {
                throw newRxError('DOC3', {
                    path
                });
            }

            const schemaObj = getSchemaByObjectPath(
                this.collection.schema.jsonSchema,
                path
            );

            if (!schemaObj) {
                throw newRxError('DOC4', {
                    path
                });

View on GitHub (pinned to af6fb65f94)

Solutions

  1. Read the primary key directly: const id = doc.primary; no observable needed.
  2. Filter the primary path out of any generated field list before calling get$.
  3. If you need a reactive value that equals the primary key, use doc.$ or an observable of the document itself and map to doc.primary.

Example fix

// before
doc.get$('id').subscribe(id => ...);
// after
import { map } from 'rxjs';
doc.$.pipe(map(d => d.primary)).subscribe(id => ...);
// or simply: const id = doc.primary;
Defensive patterns

Strategy: validation

Validate before calling

function assertNotPrimary(doc, path) {
  if (path === doc.collection.schema.primaryPath) {
    throw new Error('Primary key does not change, read it statically: doc.primary');
  }
}

Type guard

function isNonPrimaryKey(doc, path) {
  return path !== doc.collection.schema.primaryPath;
}

Try / catch

try {
  obs = doc.get$(path);
} catch (err) {
  if (String(err?.code) === 'DOC2') {
    // primary key never changes: emit it once from the static value
    import('rxjs').then(({ of }) => obs = of(doc.primary));
  } else {
    throw err;
  }
}

Prevention

When it happens

Trigger: Calling doc.get$(this.collection.schema.primaryPath), e.g. doc.get$('id'), in dev mode; passing a dynamically chosen path that happens to equal the primary path.

Common situations: Building generic field-observation helpers that iterate schema fields including the primary key; misunderstanding that the primary field needs reactivity; generating paths from a schema key list.

Related errors


AI-assisted analysis of pubkey/rxdb@af6fb65f94 (2026-08-31). Data as JSON: /api/errors/cec65b497a5fd9ce. Report an issue: GitHub.