如何使用react native将对象数组插入我的领域数据库?

时间:2019-08-01 08:26:48

标签: react-native realm

我有一个Realm数据库模型,如下所示:

export const JourneyStateSchema = {
    name : JOURNEY_STATE_SCHEMA,
    primaryKey: 'lvlNumber',    
    properties: {       
        lvlNumber: {type : 'int'},
        stateWords: {type : 'data'},
        words: {type : 'data'},
        answers: {type : 'data'},
        hints: {type : 'data?'},
    }
};

当我想插入其中时,出现此错误: JourneySchema.stateWords必须为'binary'类型,且为'object'

我不需要插入对象数组,该如何管理呢?

我的插入模型如下:

let model = {
  lvlNumber : this.lvlNumber,
  stateWords : this.state.words,//==>Array of objects
  words : this.words,//==>Array of objects
  answers : this.answers,//==>Array of objects
  hints : this.hints//==>Array of objects
 }
 insertToJourney(model).then(()=>{
 console.log(`inserted ${model} in db`)                        
 }).catch((err)=>{
    console.log(err)
 }) 

这也是我的'stateWords'数据示例:

stateWords = [
  {"value": "china", "solution": "26|36|46|56|66" , "letters" : [
    {"letter" : "c" , "find" : false},
    {"letter" : "h" , "find" : false},
    {"letter" : "i" , "find" : false},
    {"letter" : "n" , "find" : false},
    {"letter" : "a" , "find" : false},
  ]},
  {"value": "germany", "solution": "23|33|43|53|63|73|83" , "letters" : [
    {"letter" : "g" , "find" : false},
    {"letter" : "e" , "find" : false},
    {"letter" : "r" , "find" : false},
    {"letter" : "m" , "find" : false},
    {"letter" : "a" , "find" : false},
    {"letter" : "n" , "find" : false},
    {"letter" : "y" , "find" : false},
  ]},
  {"value": "france", "solution": "35|45|55|65|75|85" , "letters" : [
    {"letter" : "f" , "find" : false},
    {"letter" : "r" , "find" : false},
    {"letter" : "a" , "find" : false},
    {"letter" : "n" , "find" : false},
    {"letter" : "c" , "find" : false},
    {"letter" : "e" , "find" : false},   
  ]}
]

这是我的insertToJourney函数:

export const insertToJourney =  (item) => new Promise((resolve, reject) => {
    Realm.open(databaseOptions).then(realm => {
        realm.write(()=>{
            realm.create(JOURNEY_STATE_SCHEMA, item);
            resolve(item);
        })
    }).catch((error) => reject(error));
});

0 个答案:

没有答案