1

我有看起来像这样的数据

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"]
        }
    },
    ...
}}

如何在realmfor中编写架构react native

const personSchema = {
    name: "Person",
    properties: {
        name: string,
        age: int,
        // favorites: I don't know what to write here??
    }
}

我尝试使用类型字典(“{}”),但它给了我一个错误:

[错误:混合属性不能包含值数组。]

当我使用“混合”类型时,我得到了这个错误:

[错误:仅支持领域实例。]

我需要为此创建一个对象类型吗?如果是这样,当我不确定收藏夹中的键是什么时该怎么办?

这是我创建和编写实例的代码。

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
    }
}

还是我应该改变我写入数据库的方式?谁能帮我这个?提前致谢。

4

0 回答 0