Realm报错TypeError: item.toJSON is not a function求助
解决Realm查询时的TypeError: item.toJSON is not a function错误
问题原因
你初始化Realm时,schema参数传入的是类名字符串数组(['MaturationCode', 'Maturation']),但Realm需要的是实际的Schema定义对象或者带有schema属性的类。传字符串会导致Realm无法正确识别模型结构,生成的查询对象缺少toJSON方法,从而触发错误。
修复方案
修改openRealm方法中的schema参数,改为传入类本身或者类的schema属性:
方式1:传入类数组
public openRealm() { return new Realm({ path: 'larvia.realm', schema: [MaturationCode, Maturation], // 替换原字符串数组 schemaVersion: schemaVersion, }); }
方式2:传入Schema对象数组
public openRealm() { return new Realm({ path: 'larvia.realm', schema: [MaturationCode.schema, Maturation.schema], // 使用类的schema属性 schemaVersion: schemaVersion, }); }
两种方式都能让Realm正确加载模型定义,查询出的Maturation对象就会拥有toJSON方法,解决报错问题。
内容的提问来源于stack exchange,提问作者Ezequiel Jaramillo
相关产品推荐
相关产品推荐

