You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RxDB报错:Cannot read properties of undefined (reading 'key') 原因咨询

RxDB更新IndexedDB时的TypeError报错排查

报错信息

rx-schema-helper.ts:104 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'key')
    at getPrimaryFieldOfPrimaryKey (rx-schema-helper.ts:104:63)
    at fillWithDefaultSettings (rx-schema-helper.ts:163:33)
    at createRxSchema (rx-schema.ts:153:25)
    at rx-database.ts:280:28
    at Array.forEach (<anonymous>)
    at RxDatabaseBase2.addCollections (rx-database.ts:276:44)
    at index.ts:24:20
    at Array.map (<anonymous>)
    at creatDataBase (index.ts:22:29)

报错原因分析

从调用栈和报错信息来看,问题出在getPrimaryFieldOfPrimaryKey函数读取undefined对象的key属性,核心原因是你的RxDB Schema存在主键配置错误:

  • 未在Schema顶层明确声明primaryKey字段
  • 复合主键的配置格式错误,比如写成了无key属性的对象结构
  • 主键字段的定义遗漏,导致RxDB无法识别有效主键

RxDB要求每个集合的Schema必须指定主键:单个主键直接写字段名字符串,复合主键需要按{ key: '主键字段名', fields: ['字段1', '字段2'] }的格式配置,不符合规范就会触发该报错。

内容的提问来源于stack exchange,提问作者sanguogege

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 16:32:02