简体   繁体   English

如何将模式对象推入另一个模式对象的array属性?

[英]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. 我浏览了 文档 ,却找不到任何示例来说明如何将对象推入其父对象的array属性。

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. 为了更清楚一点,我有一个模式Test ,该Test具有属性data: {type: "data[]", default: []} ,但是我无法将任何data对象压入其中。

Error: 错误:

Here is the error I get. 这是我得到的错误。

Property must be of type 'data', got ([object RealmObject]) 属性必须为'data'类型,已获取([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 DeviceData架构

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 . data是领域的保留字,因为它已经具有数据类型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. Realm支持以下基本类型:bool,int,float,double,string,data和date。

  • bool properties map to JavaScript boolean values bool属性映射到JavaScript boolean
  • int , float , and double properties map to JavaScript number values. intfloatdouble属性映射到JavaScript数字值。 Internally int and double are stored as 64 bits while float is stored with 32 bits. 内部intdouble存储为64位,而float存储为32位。
  • string properties map to string string属性映射到string
  • data properties map to ArrayBuffer data属性映射到ArrayBuffer
  • date properties map to Date date属性映射到Date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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