cat*_*dle 6 realm database-schema react-native realm-js realm-database
我有这样的数据
PersonJSONData = {
"key1": {
"name": "odo",
"age": 10,
"favorites": {
"food": ["rice", "chocolate", "sugar"],
"game": [],
"color": ["red"]
},
"key2": {
"name": "yana",
"age": 50,
"favorites": {
"band": [],
"food": ["eggs"],
"book": ["ABC", "how to cook"]
}
},
...
}}
Run Code Online (Sandbox Code Playgroud)
如何在realmfor中写入架构react native?
const personSchema = {
name: "Person",
properties: {
name: string,
age: int,
// favorites: I don't know what to write here??
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用类型字典(“{}”),但它给了我一个错误:
[错误:混合属性不能包含值数组。]
当我使用“混合”类型时,我收到此错误:
[错误:仅支持领域实例。]
我需要为此创建一个对象类型吗?如果是这样,当我不确定收藏夹中的按键是什么时该怎么办?
这是我创建和编写实例的代码。
const PersonInstance = new Realm(schema: [personSchema] })
function writePerson(){
const personKeys = Object.keys(PersonJSONData)
try {
personKeys.forEach((key) => {
const { name, age, favorites } = PersonJSONData[key]
PersonInstance.write(() => {
PersonInstance.create('Person', {
name, age, favorites
})}
})
} catch(err) {
// error handling
}
}
Run Code Online (Sandbox Code Playgroud)
或者我应该改变写入数据库的方式?谁能帮我这个?提前致谢。
| 归档时间: |
|
| 查看次数: |
2911 次 |
| 最近记录: |