I want to create a profile page in react-native and I can not create a database without an array. I just want to have one object and update it fluenly


#1

import Realm from ‘realm’

export const ProfileSchema = {
name: ‘Profile’,
properties: {
info: {type: ‘Datas’}
}
}

export const DataProfileSchema = {
name: ‘Datas’,
properties: {
firstName: {type:‘string’, default: null},
lastName: {type:‘string’, default: null},
gender: {type:‘string’, default: null},
incomeDay: {type:‘int’, default: 1}
}
}

export const saveProfile = newProfile => new Promise((resolve, reject) => {
Realm.open({schema: ProfileSchema})
.then(realm => {
realm.create(‘Person’, newProfile)
resolve(newProfile)
}).catch(error => reject(error))
})

export const getProfile = () => new Promise((resolve,reject) => {
Realm.open({schema: ProfileSchema})
.then(realm => {
let profile = realm.objects(‘Profile’)
resolve(profile)
}).catch(error => reject(error))
})

export default new Realm({schema: ProfileSchema})


#2

A Realm schema is an array of object schemas. In your case, you need to use [ProfileSchema, DataProfileSchema] since ProfileSchema requires DataProfileSchema through Datas.

Moreover, you create a Person object, but none of the object schemas (ProfileSchema and DataProfileSchema) are named Person.