最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Storing arrays in react-native realm - Stack Overflow

programmeradmin2浏览0评论

I cant seem to store an array from a json to my realm database. Lets say the json that was returned to me is:

  contact: 
      { 
          name: 'Test Name',
          contactDetails: [ '4354354', '099324' ] 
      },

How can I insert contactDetails into realm?

Ive tried creating a custom object like this according to this How to store [String] or [Int] in react-native realm:

class stringObject extends Realm.Object {}
    stringObject.schema = {
     name: 'stringObject',
      properties: { value : 'string' }
     };

And tried adding this to the schema:

  contactDetails: {type: 'list', objectType: 'stringObject'}

But it cant be inserted into realm. I tried emptying the value in properties for my stringObject but it still doesnt work.

I cant seem to store an array from a json to my realm database. Lets say the json that was returned to me is:

  contact: 
      { 
          name: 'Test Name',
          contactDetails: [ '4354354', '099324' ] 
      },

How can I insert contactDetails into realm?

Ive tried creating a custom object like this according to this How to store [String] or [Int] in react-native realm:

class stringObject extends Realm.Object {}
    stringObject.schema = {
     name: 'stringObject',
      properties: { value : 'string' }
     };

And tried adding this to the schema:

  contactDetails: {type: 'list', objectType: 'stringObject'}

But it cant be inserted into realm. I tried emptying the value in properties for my stringObject but it still doesnt work.

Share Improve this question edited May 23, 2017 at 11:33 CommunityBot 11 silver badge asked Jul 1, 2016 at 7:23 Joshua I. TorreJoshua I. Torre 331 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

In the properties of your schema:

contactDetails: { type: 'string?[]' },

Lists in Realm contain objects so you need to transform you JSON to the correct form before insertion. For the schema you provided you need to transform contactDetails to represent an array of valid StringObject objects before calling realm.create:

contactDetails: [ '4354354', '099324' ] 

to

contactDetails: [ { value: '4354354' }, { value: '099324' } ] 

You could do this using Array.map with something like:

json.contactDetails = json.contactDetails.map( s => ({ value: s }) );
发布评论

评论列表(0)

  1. 暂无评论