我似乎不能从JSON存储数组到我的realm数据库。假设返回给我的json是:
contact:
{
name: 'Test Name',
contactDetails: [ '4354354', '099324' ]
},
如何将contactDetails插入到realm中?
我试着根据How to store [String] or [Int] in react-native realm创建一个这样的自定义对象:
class stringObject extends Realm.Object {}
stringObject.schema = {
name: 'stringObject',
properties: { value : 'string' }
};
并尝试将以下内容添加到架构中:
contactDetails: {type: 'list', objectType: 'stringObject'}
但是它不能插入到领域中。我尝试清空stringObject属性中的值,但仍然不起作用。
2条答案
按热度按时间gopyfrb31#
Realm中的列表包含对象,因此您需要在插入之前将JSON转换为正确的形式。对于您提供的模式,您需要在调用
realm.create
之前转换contactDetails
以表示有效StringObject
对象的数组:到
您可以使用
Array.map
来执行此操作,如下所示:bybem2ql2#
在方案的属性中: