No results from Realm Cloud Sync — javascript

cloud

#1

I have an existing Realm with several ‘Documents’ records hosted for a particular user in Realm Cloud.

I can write new ‘Documents’ from the client.

But trying to sync the remote documents to my local client results in zero records.

code:

      const config = currentUser.createConfiguration({
        sync: {
          url: 'realms://xx.us1.cloud.realm.io/~/default',
        },
        schema: [DocumentSchema],
      })

      Realm.open(config)
        .then((realm) => {
          const Documents = realm.objects('Documents')
          Documents.subscribe()

          // Documents.length is 0, so this never runs.
          // However, there are 9 'Documents' on Realm Cloud, created by this same client with realm.write()
          if (Documents.length) {
            each(Documents, (document) => {
              console.log('document', document.name)
            })
          }
        })
    }

#2

When you use query-based sync (Documents.subscribe()) you will have to wait for the server to create the partial Realm. You can do it by setting up a listener - see https://realm.io/docs/javascript/latest/api/Realm.Sync.Subscription.html for the API.


#3

Thanks @kneth.

I did get it working, but it’s really confusing.

Do the __Class, __Role, etc. objects automatically add listeners?

This doesn’t work without adding Documents.addListener():

          const Documents = realm.objects('Documents')
          Documents.subscribe()

But this does:

          const Classes = realm.objects('__Class')
          Classes.subscribe()

#4

__Class, etc. contains objects which are maintained automatically by ROS as they are part of the permissions system. So you don’t have to subscribe to them.


#5

Awesome. Thanks for the clarification!