realm-js: Cannot insert Array of Object

How frequently does the bug occur?

All the time

Description

I have an array of objects to be insert into realmDB, I tried to console.log() it out. It is like this

"participants": [[Object], [Object]]

// example json
[{"avatar": "https://static.chativo.io/images/avatar/001.png", "id": "5f56de52987fdb1a15615a52", "kind": "agent", "name": "1wltest4", "presence": "offline", "seen_at": "2022-01-10T10:18:49.432Z", "seen_count": 369, "visible": true}, {"avatar": "/api/v2/file/c4d8bhvt6p4e6328o8dg/IMG_20210620_115523.jpg", "id": "5f56de52987fdb1a15615a55", "kind": "agent", "name": "wltest7", "presence": "offline", "seen_at": "2022-01-10T03:51:05.005Z", "seen_count": 351, "visible": true}]

Here is my code

const RoomSchema = {
  name: 'Room',
  primaryKey: 'id',
  properties: {
    participants: 'Person[]'
  },
};

const PersonSchema = {
  name: 'Person',
  properties: {
    id: 'string?',
    kind: 'string?',
    name: 'string?',
    seen_count: 'int?',
    avatar: 'string?',
    presence: 'string?',
    visible: 'bool?'
  }
};

However, when I try to insert, it keeps throw me error.

TypeError: undefined is not an object (evaluating 'realm.write')

Please help if I done anything wrong.

Stacktrace & log output

TypeError: undefined is not an object (evaluating 'realm.write')

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.11.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Android 10

Build environment

Which debugger for React Native: … Flipper

Cocoapods version

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

@WenLonG12345 No, it behaves like a singleton. But be careful about realm.close() as this will close all instances.

I’ll expand on this. If you call new Realm or Realm.open with the same configuration, internally it will return the same Realm instance from the first call. However it is not doing any reference counting, so a call to realm.close() will make that instance close everywhere it is referenced.

@WenLonG12345 If you really need to access the realm outside of a component, then you can create a new instance with the same configuration.

const realm = await Realm.open(sharedConfig);