如何在Realm中添加嵌套的对象列表"错误:JS值必须是类型:object"

时间:2016-07-06 00:56:46

标签: javascript json react-native realm realm-list

我试图创建具有json对象数组的Realm数据库,这些对象具有嵌套的对象数组。

当我尝试使用下面的代码添加时,我总是得到错误:JS值必须是类型:object。

架构:

String searchText = "AppraisersGroupTest";
WebElement dropdown = driver.findElement(By.id("grdAvailableGroups"));
dropdown.click(); // assuming you have to click the "dropdown" to open it
List<WebElement> options = dropdown.findElements(By.tagName("li"));
for (WebElement option : options)
{
    if (option.getText().equals(searchText))
    {
        option.click(); // click the desired option
        break;
    }
}

以及我尝试创建数据库的方法:

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]});

我尽可能地尝试,将数组直接放在练习创作中,等等,我没有成功..

干杯

1 个答案:

答案 0 :(得分:1)

你必须指定记录的索引。由于exercise.返回的记录不是运动对象

试试这个

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'
    });

});