4

I'm trying to create Realm database that has a json array of objects with a nested array of objects.

When I try to add using the code below I always get the error: JS value must be of type: object.

Schemas:

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..

Cheers

4

1 回答 1

4

U have to specify the index of the record. As exercise.returns a record not an exercise object

try this instead

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 回答