Skip to content

fix(utils): skip shared records whose id is missing - #895

Open
aiumz wants to merge 1 commit into
worktile:developfrom
aiumz:fix/skip-shared-records-without-id
Open

aiumz wants to merge 1 commit into
worktile:developfrom
aiumz:fix/skip-shared-records-without-id

Conversation

@aiumz

@aiumz aiumz commented Nov 10, 2026

Copy link
Copy Markdown

Summary

 getRecordsBySharedJson
-  read record[0][0]._id
-  read every field._id
+  if record or field id is missing
+    skip that entry
+  keep the remaining rows and cells

表格内容从 Yjs JSON 还原时,某条记录的系统字段或某个字段没有 _id,getRecordsBySharedJson 会抛出 Cannot read properties of undefined (reading '_id'),整页内容读取失败。现在跳过这些记录和字段,其余数据继续返回。

Evidence

  • Before: 系统字段第 0 项为空时,systemFieldValues[0]._id 抛错,后续记录不再转换。
    After: 同样的数据只丢掉这条记录。字段数组中间有空项时,取值下标不变,后面的单元格仍对到原字段。
before: throw reading _id
after: [{"_id":"r1","values":{"f1":"a","f2":"b"}},{"_id":"r2","values":{"f1":null,"f2":9}}]

Merge Danger

Door: two-way

回滚这次提交即可恢复原读取逻辑。

Blast Radius: table-content

正常记录的还原结果不变。缺 _id 的记录会被跳过,调用方拿到的行数可能变少。getIdBySystemFieldValues 的返回类型变为 string | undefined。

Made with Cursor

Converting table content threw when a record or field had no _id, which failed the whole page. Skip those entries and keep the remaining rows.

Co-authored-by: Cursor <cursoragent@cursor.com>

@Xwatson Xwatson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改法有几个问题,暂时不建议合:

  1. PR 没有说明缺 _id 的数据是怎么来的。gaea 之前排查过表格读取报错,记录在 src/tools/yjs/incomplete-table-redis-cache.md:Redis 里只剩增量、缺了基础更新时,还原出来的表格内容就是残缺的。那次修的是 Redis key 过期这个源头,也明确决定不加读取兜底。现在在共享库里跳过缺 _id 的记录,等于把这类问题藏起来了。比如文档里那次 records 为 undefined 的情况,加了下面这段之后会直接返回一张空表,不再报错:

    if (!Array.isArray(recordJsonArray)) {
        return [];
    }
  2. 跳过记录以后,前端 state 和 Yjs 里 records 的下标就对不上了。记录排序是按下标定位的:

    // move-records.ts,用的是 state 里的下标
    Actions.setRecordPositions(aiTable, { [activeViewId]: positions[index] }, [sourceIndex]);
    // add-node.ts,拿这个下标去 Yjs 里取记录
    const record = records.get(recordIndex);

    被跳过的那条记录后面的行,拖动时排序值会写到 Yjs 里的另一条记录上,并同步给其他协作者、写进库里。原来只是这个页面读不出来,现在会把其它正常的行改坏。add-node.ts 里加的 continue 也是在补这种不一致,但按下标定位的地方不止这一处。

  3. gaea 也用 @ai-table/utils 的 getRecordsBySharedJson 读表格最新内容。升级后,分享页、关联、Excel/CSV 导出、另存为模板拿到的都会是残缺数据,而且另存为模板会把残缺内容存下来。

  4. getIdBySystemFieldValues 兼容了第 0 项是字符串、systemFieldValues 不是数组的情况,getValuesByCustomFieldValues 等函数还兼容了 record 不是数组、字段为空。这些格式类型定义里没有(SystemFieldValues = [{ _id: string }, ...]),也没有数据能证明会出现,建议去掉。

建议先确认缺 _id 的数据从哪来:是哪个页面,读取时 Redis 和 Mongo 里的 updates 分别是什么情况。如果是缺了基础更新,应该在 gaea 读取的时候处理,比如 Redis 单独还原时有未满足的依赖,就先合并 Mongo 的 updates 再读。共享库这里还是让它报错,不要让 state 和 Yjs 的记录数不一致。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants