【问题标题】:How to add a nested List of objects in Realm "Error: JS value must be of type: object"如何在领域中添加嵌套的对象列表“错误:JS值必须是类型:对象”
【发布时间】:2016-07-06 00:56:46
【问题描述】:

我正在尝试创建具有 json 对象数组和嵌套对象数组的 Realm 数据库。

当我尝试使用下面的代码添加时,我总是得到错误: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]});

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

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

    });

我尝试了所有可能的方法,将数组直接放在练习创建等中,我没有成功..

干杯

【问题讨论】:

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


    【解决方案1】:

    你必须指定记录的索引。正如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'
        });
    
    });
    

    【讨论】:

    • 感谢您的帮助,我在项目中采用了另一种方式,因此无法测试它是否有效,但感谢您的宝贵时间
    • 如果可能的话,您可以自己检查并标记为正确答案
    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多