How to add a nested List of objects in Realm "Error: JS value must be of type: object"
How to add a nested List of objects in Realm "Error: JS value must be of type: object"
我正在尝试创建具有 json 对象数组和嵌套对象数组的 Realm 数据库。
当我尝试使用下面的代码添加时,我总是得到错误:JS 值必须是类型:对象。
模式:
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]});
以及我尝试创建数据库的方法:
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'
});
});
各种方法都试过了,直接把数组放在练习创建中等等,都没有成功..
干杯
你必须指定记录的索引。作为 exercise.
returns 记录而不是练习对象
试试这个
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'
});
});
我正在尝试创建具有 json 对象数组和嵌套对象数组的 Realm 数据库。
当我尝试使用下面的代码添加时,我总是得到错误:JS 值必须是类型:对象。
模式:
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]});
以及我尝试创建数据库的方法:
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'
});
});
各种方法都试过了,直接把数组放在练习创建中等等,都没有成功..
干杯
你必须指定记录的索引。作为 exercise.
returns 记录而不是练习对象
试试这个
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'
});
});