如何将模式对象推送到另一个模式对象的数组 属性 中?
How to push a schema object into the array property of another schema object?
描述
我浏览了 realm-js documentation,但找不到任何示例来解释如何将对象推入其父对象的数组 属性。
更清楚一点,我有一个架构 Test
,其中有一个 属性 data: {type: "data[]", default: []}
,但是我无法将任何 data
对象推送到它.
错误:
这是我得到的错误。
Property must be of type 'data', got ([object RealmObject])
我尝试了什么:
这是我试过的:
this.realm.write(()=>{
const dataObj = this.realm.create('data', data);
this.user.test.data.push(dataObj);
})
我做错了什么?
我也试过直接push数据,也报了类似的错误
测试架构:
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;
设备数据架构
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
是领域的保留字,因为它已经有一个 data type 作为 data
。如果将架构名称更改为其他名称,问题将得到解决。
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
描述
我浏览了 realm-js documentation,但找不到任何示例来解释如何将对象推入其父对象的数组 属性。
更清楚一点,我有一个架构 Test
,其中有一个 属性 data: {type: "data[]", default: []}
,但是我无法将任何 data
对象推送到它.
错误:
这是我得到的错误。
Property must be of type 'data', got ([object RealmObject])
我尝试了什么:
这是我试过的:
this.realm.write(()=>{
const dataObj = this.realm.create('data', data);
this.user.test.data.push(dataObj);
})
我做错了什么?
我也试过直接push数据,也报了类似的错误
测试架构:
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;
设备数据架构
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
是领域的保留字,因为它已经有一个 data type 作为 data
。如果将架构名称更改为其他名称,问题将得到解决。
Realm supports the following basic types: bool, int, float, double, string, data, and date.
bool
properties map to JavaScriptboolean
valuesint
,float
, anddouble
properties map to JavaScript number values. Internallyint
anddouble
are stored as 64 bits whilefloat
is stored with 32 bits.string
properties map tostring
data
properties map toArrayBuffer
date
properties map toDate