New to Realm, problem creating sync subscription

I’ve read a few similar posts here and the docs but I’m still missing something. I get the following error when trying to create/write my first Realm object to my DB:

RealmError: Cannot write to class Project when no flexible sync subscription has been created.

I was reading the Node SDK and just following along with it to understand how to create a subscription. I’m not sure I need to actually create a filtered subscription that looks for objects with current user’s ID in the owner field but I felt like that’s a reasonable way to get started.

I have no data in the DB for this object, this is my first attempt at running Realm. Thanks for any help… But, it seems I’m creating a subscription and I have flexible sync / dev mode enabled in the backend.

Here’s what I have so far:

Screenshot 2023-06-13 at 8.11.20 PM

Screenshot 2023-06-13 at 8.11.37 PM

Can you try using the subscriptions update API instead of using initialSubscriptions? Follow the guidelines from the docs here: https://www.mongodb.com/docs/realm/sdk/node/sync/flexible-sync/#std-label-node-sync-subscribe-to-queryable-fields

Specifically, update the subscriptions using something like this:

await realm.subscriptions.update((mutableSubs) => {
  mutableSubs.add(longRunningTasks, {
    name: "longRunningTasksSubscription",
  });
  mutableSubs.add(bensTasks);
  mutableSubs.add(realm.objects("Team"), {
    name: "teamsSubscription",
    throwOnUpdate: true,
  });
});
1 Like

I did just test the initial subscriptions syntax that you’re using, and it appears to work. Can you also confirm the version of the realm SDK that you’re using?

How do I check version of my SDK? I just did an npm install realm about 2 weeks ago or so.

As far as the subscriptions stuff, the code you referenced should that be run immediately after I run the open method?

You can check your package.json - you should have a line line "realm": "^11.9.0",.

And yes, after await realm.open you can run await realm.subscriptions.update

1 Like

“version”: “11.9.0”,

Ok, I’ll give it a try. Also, what is the syntax to create a subscription that says “just give me all objects in a collection”. That would be more helpful right now as I learn Realm, how it works, verifying the collections/data I read/write from my app, etc…

try realm.objects("TableName").filtered("truepredicate")

1 Like

When you say true predicate, you mean something like “1 == 1”, just any expression that evaluates to true so that I get access to all documents in a collection?

No I mean the literal string "truepredicate", in quotes, as your query

1 Like

If I may make a suggestion?

Since you’re new to Realm and really never used it before, I suggest starting work a local Realm first, and then once you have some experience with the Realm “ecossystem” expand into Flexible Sync and Sync’ing in general. Here’s a good starting place

My suggestion is because trying to wrap your brain around Flex Sync, and at the same time learning the SDK and usage can be somewhat overwhelming - especially when trying to determine what subscriptions you’ll actually need and what you won’t.

If you start with understanding Realm, when it comes time to plan your sync strategy, it will flow much easier and be less time consuming.

Just a thought

Welcome to Realm!

Jay

1 Like

Ok excellent, moving the subs code into a step after the open, it now works. I verified that my test write (create a project object) worked and I can see it in my DB… very good.

Thanks a ton, pasting code below as it might help someone learning Realm as I am.

Thanks @Jay regarding ‘start slow’ advice. I agree… I’ll probably stop worrying about sync’ing to my Atlas instance for awhile and just try to pick up speed on how to CRUD locally with Realm, do queries, learn as I go, etc… But, I’ve spent the past couple weeks reading a ton and trying the basics so I’m getting past crawl phase at this point. Was a little bumpy but all new learning curves always are…

const Realm = require('realm');
const { SchemaProject } = require('../models/SchemaProject.js');
const { ModelProject } = require('../models/ModelProject.js');
const { ControllerEventBus } = require('./ControllerEventBus.js');

class ControllerDatabase
{
    constructor()
    {
        this.realmApp = null;
        this.db = null;
        this.user = null;
        this.config = null;
    }
    Close()
    {
        this.db.close();
    }
    Write()
    {
        /**
         * Test write
         */
        let project = new ModelProject();
        project.title = "Test project";
        project.name = "Project name";
        project.project = "foo";
        project.owner = this.user.id;
        var projectRealm = null;
        this.db.write(() => 
        {
            projectRealm = this.db.create("Project", project);
        });
        console.log(projectRealm.toString());
    }
    async Init()
    {
        try
        {
            this.realmApp = new Realm.App({id: "APP"});
            //const anonymousUser = await app.logIn(Realm.Credentials.anonymous());
            const credentials = Realm.Credentials.emailPassword(
                "USER",
                "PW"
            );
            this.user = await this.realmApp.logIn(credentials);
            this.config = 
            {
                schema: [SchemaProject],
                sync: 
                {
                    user: this.user,
                    flexible: true               
                }
            };    
            this.db = await Realm.open(this.config);
            const query = this.db.objects("Project").filtered("truepredicate");
            await this.db.subscriptions.update((mutableSubs) => {
                mutableSubs.add(query, {name: "allProjects"});
            });

            global.deep.EventBus.Dispatch(ControllerEventBus.EVENT_DB, {"state": ControllerEventBus.EVENT_DB_READY });
        }
        catch(e)
        {
            console.log("Database exception is: " + e.toString());            
        }     
    }
}
exports.ControllerDatabase = ControllerDatabase;

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.