简体   繁体   中英

How to push a schema object into the array property of another schema object?

Description

I went through the documentation and was unable to find any examples explaining how to push an object into the array property of its parent.

To be a little more clear, I have a Schema Test which has a property data: {type: "data[]", default: []} , however I am unable to push any data objects to it.

Error:

Here is the error I get.

Property must be of type 'data', got ([object RealmObject])

What I tried:

This is what I tried:

this.realm.write(()=>{
  const dataObj = this.realm.create('data', data);
  this.user.test.data.push(dataObj);
})

What am I doing wrong?

I also tried to directly push the data directly, but I get a similar error.

Test Schema:

class Test{
}

Test.schema = {
    name: "test",
    primaryKey: "id",
    properties: {
        id: "string",
        start: "date?",
        duration: "int", //in seconds
        capsule_id: "string",
        creation: "date",
        status: "int",
        height: "float",
        weight: "float",
        time_of_evolution: "string",
        treatment: "bool",
        data: {type: "data[]", default: []},
        symptoms: {type: "symptom[]", default: []},
        meals: {type: "meal[]", default: []},
        device: "device?",
        ph11: "int?",
        ph71: "int?",
        ph12: "int?",
        ph72: "int?",
        cardinal_symptoms: {type: "cardinal_symptom[]", default: []},
    }
};

export default Test;

DeviceData Schema

class DeviceData{}

DeviceData.schema = {
    name: 'data',
    primaryKey: "timestamp", //check to see if this is a good idea
    properties: {
        ph1: 'int',
        ph2: 'int',
        x: 'int',
        y: 'int',
        z: 'int',
        timestamp: 'int',
        raw: 'string' //base64, incase something went wrong
    }
};

export default DeviceData;

data is a reserved word for realm since it already has a data type as data . If the schema name changed to something else problem will be solved.

Realm supports the following basic types: bool, int, float, double, string, data, and date.

  • bool properties map to JavaScript boolean values
  • int , float , and double properties map to JavaScript number values. Internally int and double are stored as 64 bits while float is stored with 32 bits.
  • string properties map to string
  • data properties map to ArrayBuffer
  • date properties map to Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM