Skip to content

Accessing properties of the model instance in toSnapshotProcessor throws error #560

@Tucker-Eric

Description

@Tucker-Eric

Trying to access any properties from the modelInstance in the toSnapshotProcessor fails with the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'id')
    at BaseModel.id (sharedInternalModel.ts:36:19)
    at get idAndText (field.ts:56:20)
    at toSnapshotProcessor (field.ts:50:43)
    at Array.toSnapshotProcessor (sharedInternalModel.ts:323:12)
    at chainedFn (chainFns.ts:11:25)
    at transformFn (tweakPlainObject.ts:116:29)
    at internal.ts:81:44
    at executeAction (action.ts:70:19)
    at setNewInternalSnapshot (action.ts:50:16)
    at tweakPlainObject (tweakPlainObject.ts:120:3)

It looks like the model isn't finalized and modelInstance.$ is still undefined when the model is snapshotted?

Minimal reproduction:

@model("todoSample/Todo")
export class Todo extends Model(
  {
    id: idProp,
    text: tProp(types.string),
  },
  {
    toSnapshotProcessor(sn, modelInstance) {
      return { ...sn, idAndText: modelInstance.idAndText };
    },
  },
) {
  @computed
  get idAndText() {
    return `${this.id} - ${this.text}`;
  }

  @modelAction
  setText(text: string) {
    this.text = text;
  }
}

const todo = new Todo({ text: "Hello" });
// Never able to make it here:
const output = getSnapshot(todo);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions