Realm React Native-需要迁移:属性"emailAddresses"的目标类型“字符串"不存在 [英] Realm React Native - Migration is required: Target type 'string' doesn't exist for property 'emailAddresses'

查看:173
本文介绍了Realm React Native-需要迁移:属性"emailAddresses"的目标类型“字符串"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用React Native构建一个小型应用程序,并希望使用领域来保持持久性.

I am building a small app with React Native and want to use Realm for persistency.

我为Person定义了以下架构:

const personSchema = {
  name: 'Person',
  properties: {
    familyName: {type: 'string', optional: true},
    givenName: {type: 'string'},
    middleName: {type: 'string', optional: true},
    emailAddresses: {type: 'list', objectType: 'string'},
  }
}
export class Person {}
Person.schema = personSchema

有关如何定义领域架构的信息,可以在领域文档中找到.

The information as to how a Realm schema needs to be defined can be found in the Realm docs.

然后我像这样实例化数据库:

I then instantiate the DB like so:

const schemas = [Person]
const db = new Realm({schema: schemas})

但是,到达最后一行时出现异常,给我以下错误:

However, there is an exception when reaching the last line giving me the following error:

未处理的JS异常:由于以下原因,需要迁移 错误:

Unhandled JS Exception: Migration is required due to the following errors:

  • 属性emailAddresses的目标类型string不存在.
  • Target type string doesn't exist for property emailAddresses.

我正在使用iOS模拟器进行测试,并多次删除了该应用程序,然后使用Xcode中的播放按钮将其重新安装.

I am using the iOS simulator for testing and deleted the app several times and then reinstalled it using the play button in Xcode.

有人知道为什么我会收到此异常吗?

Does anyone have an idea why I am getting this exception?

我现在为另一个数据库对象创建了一个单独的架构:EmailAdress

I now created a separate schema for another db object: EmailAdress

const emailAddressSchema = {
  name: 'EmailAddress',
  properties: {
    label: 'string',
    email: 'string'
  }
}
export class EmailAddress {}
EmailAddress.schema = emailAddressSchema

我现在也将personSchemaemailAddressesobjectType更改为EmailAddress类型:

I also changed the objectType of emailAddresses in the personSchema to be of type EmailAddress now:

...
emailAddresses: {type: 'list', objectType: 'EmailAddress'},
...

现在,我不再遇到异常了.

Now, I am not getting the exception any more. Isn't it possible to add a property to a Realm class that is a list of strings?

推荐答案

Realm尚不支持基元列表.直到它们被使用之前,您需要像使用EmailAddress

Lists of primitives are not yet supported in Realm. Until they are you need to wrap primitive types in an object as you have done with EmailAddress

这篇关于Realm React Native-需要迁移:属性"emailAddresses"的目标类型“字符串"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆