4

オブジェクトのネストされた配列を持つオブジェクトの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'
        });

    });

演習の作成に配列を直接配置するなど、可能な限りあらゆる方法を試しましたが、成功しませんでした..

乾杯

4

1 に答える 1

4

レコードのインデックスを指定する必要があります。Asexercise.は演習オブジェクトではなくレコードを返します

代わりにこれを試してください

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

});
于 2016-07-27T10:14:55.237 に答える