Auth is okay but the collection returned is empty on a React Native app that use Realm Sync

Hello,

TL;DR anonymous auth is OK, but an empty collection is returned

NB: I cannot post more than one image in this post because I’m a new user. Please follow the link to open the images. Thanks.

I’m new to Realm and I try to develop a React Native app that will display a list of contacts. Before this app, I’ve successfully testes the Task demo app.

I use a Realm function with a trigger that periodically:

  • fetch contacts from an LDAP API
  • create, update or delete contacts in an Altas collection

For now, each users of the mobile app should see the same list of contacts. Therefore, I’ve set the _partition key to public in my Contact document.

I think that my Realm app is configured correctly:

  • Schema is correctly configured (and match the schema in the client)
  • Sync is enabled and permissions are set to read only

I’ve enabled the anonymous authentication. Authentication is successful because I can see the anonymous users in Realm UI. Therefore, I know that my code is okay regarding the auth.

The React Native code is for now really simple:

import Realm from 'realm';

const app = new Realm.App({
    id: 'xxxxx',
});

const credentials = Realm.Credentials.anonymous();
const user = await app.logIn(credentials);

const realm = await Realm.open({
    schema: [Contact.schema],
    sync: {
        user,
        partitionValue: 'public',
    },
});

const contacts = realm.objects('Contact');
console.log('contacts', contacts);

And I’ve defined the following model:

import Realm from 'realm';

class Contact extends Realm.Object {
    static schema: Realm.ObjectSchema = {
        name: 'Contact',
        primaryKey: '_id',
        properties: {
            _id: 'objectId',
            _partition: 'string',
            displayName: 'string',
            firstName: 'string',
            lastName: 'string',
            thumbnail: 'string?',
            username: 'string',
            countryName: 'string?',
            countryCode: 'string?',
            jobTitle: 'string?',
            jobAffiliate: 'string?',
            jobDepartment: 'string?',
            jobOffice: 'string?',
            email: 'string',
            phone: 'string?',
            mobilePhone: 'string?',
        },
    };
}

export default Contact;

The problem is that the collection is always empty.

Realm UI logs return that the Sync is working:

see image _7e96aa7acfb2b8ec1fd4bc7badd69939157b272a hosted at ImgBB — ImgBB

Did I understand the partition system correctly? By setting the partitionValue parameter to public in my client app, it should returns the documents in my Contact collection that have _partition: "public", right?

Is it possible to use the anonymous authentication with Sync?

Did I miss something?

Thanks!

Hello @GuillaumeAp,

Welcome to the Community Forums :smiley:

The problem is that the collection is always empty.

Your code seems correct, Could you try these two options:

  1. Could you add a log statement after you run the query and see if you get the number of contacts eg console.log("There are ${objects.length}");
  2. Add a delay of 20-30 seconds before you execute the statement to display contacts.

Did I understand the partition system correctly? By setting the partitionValue parameter to public in my client app, it should returns the documents in my Contact collection that have _partition: "public" , right?

Yes, your understanding is correct, partition is a way of filtering the data per user. You can choose any partition strategy that suits your use case.

Is it possible to use the anonymous authentication with Sync?

Yes, it is possible to use but is not recommended. This means anyone can log in to your app and read the data available. Also, the data is not persisted on the device once the user logs out. To persist data locally, you should use a different authentication provider.

I hope the provided information helps. I look forward to your response.

Kind Regards,
Henna