Restore the history doc from snapshot

I am trying to restore the doc from latest snapshot, this is the function look like:

async storeSnapshot(syncFileAttr: SyncFileAttr, doc: Y.Doc) {
    if (typeof window !== "undefined") {
      return;
    }
    if (!this.pool) {
      return;
    }
    try {
      const latestSnapshot = await getFileLatestSnapshot(syncFileAttr.docName);
      const latestClock = await getCurrentUpdateClock(syncFileAttr.docName);
      const snapshot: Y.Snapshot = Y.snapshot(doc);
      const encoded = Y.encodeSnapshot(snapshot);
      const curContent = doc.getText(syncFileAttr.docName).toString();
      const prevSnapshot = latestSnapshot
        ? Y.decodeSnapshot(new Uint8Array(latestSnapshot.value))
        : null;
      const diff = latestSnapshot
        ? this.getSnapshotDiff(snapshot, prevSnapshot!)
        : "";
      let prevContent = "";
      let prevContent1 = "";
      if (prevSnapshot && latestSnapshot) {
        const originDoc = new Y.Doc({ gc: false });
        const prevDoc = Y.createDocFromSnapshot(originDoc, prevSnapshot);
        const prevDoc1 = Y.createDocFromSnapshot(doc, prevSnapshot);
        prevContent = prevDoc.getText(syncFileAttr.docName).toString();
        prevContent1 = prevDoc1.getText(syncFileAttr.docName).toString();
        logger.info("prevContent", prevContent);
        logger.info("prevContent1", prevContent1);
      }
      const client = await this.pool.connect();
      try {
        await client.query("BEGIN");
        const key = `snapshot_${syncFileAttr.docName}_${Date.now()}`;
        await client.query(
          `INSERT INTO tex_sync_history 
            (key, value, version, content_type, doc_name, clock, source, project_id, created_time, diff) 
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), $9)`,
          [
            key,
            encoded,
            "1.0", // version
            "snapshot",
            syncFileAttr.docName,
            latestClock,
            "system",
            syncFileAttr.projectId,
            diff,
          ]
        );

        await client.query("COMMIT");
      } catch (error) {
        await client.query("ROLLBACK");
        logger.error("storeSnapshot error", error);
        throw error;
      } finally {
        client.release();
      }
    } catch (error) {
      logger.error("Failed to store snapshot:", error);
      throw error;
    }
  }

for a peroid of time, I will store the snapshot into database, I also want to store the diff into database so I can tell the user the changed content for each version. I can get the current full text from doc. then I want to restore the lastest version from snapshot that I stored before. The problem is that when I use a new doc, the precontent is null, when I use the latest doc, it will show this error:

[2025-07-03T13:50:18.154] [ERROR] default - Failed to store snapshot: TypeError: Cannot read properties of undefined (reading 'id')
│     at findIndexSS (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2910:22)
│     at file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2783:31
│     at transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:3452:14)
│     at Doc.transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:545:12)
│     at Module.createDocFromSnapshot (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2766:13)
│     at PgHisotoryPersistance.storeSnapshot (file:///home/node/app/dist/storage/adapter/postgresql/pg_history_persistance.js:136:35)
│     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
│     at async file:///home/node/app/dist/storage/appfile.js:19:5
│ [2025-07-03T13:50:18.154] [ERROR] default - uncaughtException TypeError: Cannot read properties of undefined (reading 'id')
│     at findIndexSS (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2910:22)
│     at file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2783:31
│     at transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:3452:14)
│     at Doc.transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:545:12)
│     at Module.createDocFromSnapshot (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2766:13)
│     at PgHisotoryPersistance.storeSnapshot (file:///home/node/app/dist/storage/adapter/postgresql/pg_history_persistance.js:136:35)
│     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
│     at async file:///home/node/app/dist/storage/appfile.js:19:5 unhandledRejection TypeError: Cannot read properties of undefined (reading 'id')
│     at findIndexSS (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2910:22)
│     at file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2783:31
│     at transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:3452:14)
│     at Doc.transact (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:545:12)
│     at Module.createDocFromSnapshot (file:///home/node/app/node_modules/rdyjs/dist/yjs.mjs:2766:13)
│     at PgHisotoryPersistance.storeSnapshot (file:///home/node/app/dist/storage/adapter/postgresql/pg_history_persistance.js:136:35)
│     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
│     at async file:///home/node/app/dist/storage/appfile.js:19:5

am I missing something? what should I do to fixed this error?