Not able to sincronize nor write information to Realm


#1

Hi, i’m currently having an issue, i’m trying to sync to realm using javascript and is not being possible, i see that the schema is being created in ROS but it’s empty.

am i mising something? or doing something wrong?

thanks a lot in advance.

this is the code…

//Schemas
const Weapons = {
name: ‘Weapons’,
properties: {
weaponName: ‘string’,
owner: ‘string’
}
};

const Superheroes = {
name: ‘Superheroes’,
properties: {
name: ‘string’,
superPower: ‘string’,
weapon: ‘string’
}
};

//ROS URL
const authUrl = https://${constants.serverUrl};
let credentials = Realm.Sync.Credentials.usernamePassword(constants.username, constants.password);

//Authenticating
Realm.Sync.User.login(authUrl, credentials).then(user => {
console.log("Server URL: " + authUrl);

//Setting the config against the Realm obj
let config = user.createConfiguration({
    sync: {
        user: user,
        url: `realms://${constants.serverUrl}/node-js-test`,
        error: (error) => {
            console.log(error.name, error.message);
        },
        //schema: [Weapons, Superheroes]
    }
});
console.log("Realm URL: " + config.sync.url);
console.log("User: " + config.sync.user);

//Opening the specified Realm
Realm.open(config).then(realm => {
    console.log("Realm opened...");
    
    //Writing data into the Schemas
    realm.write(() => {
        console.log("Starting to write...");

        //'Weapons' Schema
        realm.create('Weapons', {
            weaponName: 'Thor Hammer',
            owner: 'Thor'
        });
        //'Superheroes' schema
        realm.create('Superheroes', {
            name: 'Thor',
            superPower: 'Lightning',
            weapon: 'Hammer'
        });
    })

    realm.close();

}).catch(e => console.log(e));

}).catch(e => console.log(e));


#2

I think you are closing the Realm (and the sync session) too early, and the objects haven’t time to get uploaded. Let me suggest that you call Realm.Sync.Session.uploadAllLocalChanges() and wait for it to complete before closing your Realm.


#3

Hi Kneth thanks for the response! unfortunately the method that you suggested does not do the trick

const Realm = require('realm');

//Schemas
const Weapons = {
    name: 'Weapons',
    properties: {
        weaponName: 'string',
        owner: 'string'
    }
};

const Superheroes = {
    name: 'Superheroes',
    properties: {
        name: 'string',
        superPower: 'string',
        weapon: 'string'
    }
};

//setting the credentials
var cred = Realm.Sync.Credentials.usernamePassword("admin", "123");
console.log("credentials set...");

//setting the login
Realm.Sync.User.login("https://testinstancerealm.us1.cloud.realm.io", cred).then(currentUser => {  

    console.log("logging user...");
    let config = currentUser.createConfiguration();
    config.schema = [Weapons];
    config.sync.user = currentUser;
    config.sync.url = "realms://testinstancerealm.us1.cloud.realm.io/node-js-test_1";
    config.sync.fullSynchronization = true;
    config.sync.validate_ssl = false;
    console.log("Schema: " + config.schema.name + "\n" +
        "User: " + config.sync.user.name + "\n" +
        "Url: " + config.sync.url + "\n" + 
        "FullSync: " + config.sync.fullSynchronization + "\n" + 
        "Validate SSL: " + config.sync.validate_ssl + "\n" + "config done...\n");

    //open Realm with previously config object
    Realm.open(config).then(realm => {
        console.log("realm opened with configs...")
        let weapons = realm.objects('Weapons');

        realm.write(() => {
            var weapon = realm.create('Weapons',{
                owner: 'Cap',
                weaponName: 'Shield'
            }, true)
        })

        realm.commitTransaction();

        Realm.Sync.Session.uploadAllLocalChanges().then(()=>{
            realm.close();
        }).catch(error => console.log(error));
    
    }).catch(error => {
        console.log(error);
    });

}).catch(error => {
    console.log(error);
})
    

here there is again the snippet with the added piece of code… this is driving me crazy since i cannot find the solution…


#4

here is a screenshot from the console, maybe this helps…

it seems that it never got passed the Realm.open(config).


#5

config.schema.name returns undefined as config.schema is an array. Moreover, Realm.Sync.User doesn’t have a property called name.

Please enable the sync logger (Realm.Sync.setLogger('all')) to get a better understanding of why Realm.open() hangs.


#6

I’m new to Realm…could you please tell where in the code and how i insert that statement? thanks a lot in advance!


#7

You can do something like:

const Realm = require('realm'); 
Realm.Sync.setLogger('all');
// Schemas 
const Weapons = { name: 'Weapons', properties: { weaponName: 'string', owner: 'string' } };

#8

it’s giving me this…


#9

Sorry, I meant Realm.Sync.setLogLevel('all').


#10

now we got the logs…here’s what i got…