2016-07-06 6 views
1

オブジェクトのネストされた配列を持つオブジェクトのjson配列を持つレルムデータベースを作成しようとしています。レルム内のオブジェクトのネストされたリストを追加する方法 "エラー:JS値はタイプ:オブジェクトでなければなりません"

以下のコードを使用して追加しようとすると、私はいつもエラーが発生します:JS値は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' 
     }); 

    }); 

私はなど、運動の作成に配列を直接入れて、可能なあらゆる方法を試してみました、私は成功しなかった。

乾杯

答えて

1

Uはレコードのデクス。 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' 
    }); 

}); 
+1

感謝をしてみてください、私はそれが仕事をしたりされていない場合、私のプロジェクト内の別の方法は、私がテストをカント行きましたが、私はあなたの時間を感謝 –

+0

可能であれば、あなた自身のためにそれをチェックし、それを正解とマークすることができます –

関連する問題