如何在Realm中添加嵌套的对象列表“错误:JS值必须是类型:object” [英] How to add a nested List of objects in Realm "Error: JS value must be of type: object"
本文介绍了如何在Realm中添加嵌套的对象列表“错误:JS值必须是类型:object”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用嵌套的对象数组创建具有json对象数组的Realm数据库。
I'm trying to create Realm database that has a json array of objects with a nested array of objects.
当我尝试使用下面的代码添加时总是得到错误:JS值必须是类型:object。
When I try to add using the code below I always get the error: JS value must be of type: object.
架构:
import Realm from 'realm';
class Exercise extends Realm.Object {
}
Exercise.schema = {
name: 'Exercise',
primaryKey: 'id',
properties: {
id: 'int',
name: 'string',
category: 'string',
bodyPart: 'string',
levels: {type: 'list', objectType: 'Level'}
}
};
class Level extends Realm.Object {
}
Level.schema = {
name: 'Level',
properties: {
level: 'int',
equipments: 'string'
}
};
export default new Realm({schema: [Exercise, Level, Multiplier]});
以及我正在尝试创建数据库的方法:
and the method where I'm trying to create the database:
realm.write(() => {
let exercise = realm.create('Exercise', {
id: 209,
name: 'Dumbbell Overhead Press',
category: 'Military Press',
bodyPart: 'Shoulder'
}, true);
exercise.levels.push({
level: 3,
equipments: 'DB'
});
});
我尽可能地尝试,将数组直接放在练习创建等中,我没有成功..
I tried every way possible, putting the array direct in the Exercise creation, etc, I had no success..
干杯
推荐答案
你必须指定索引记录。作为运动。
返回记录而不是运动对象
U have to specify the index of the record. As exercise.
returns a record not an exercise object
尝试这样做
realm.write(() => {
let exercise = realm.create('Exercise', {
id: 209,
name: 'Dumbbell Overhead Press',
category: 'Military Press',
bodyPart: 'Shoulder'
}, true);
exercise[0].levels.push({
level: 3,
equipments: 'DB'
});
});
这篇关于如何在Realm中添加嵌套的对象列表“错误:JS值必须是类型:object”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文