Problem opening my first Realm on an object server

docs

#1

I am trying to follow the documentation given here: https://realm.io/docs/javascript/latest/

I can redo the examples (run them on my own node.js) until I get to the “Opening Realm” section. Here is the code, I have made trying to imitate the documentation: (replacing my real user name and realm cloud url with …)

const Realm = require(‘realm’);

// Define Tingle model and properties
const ThingSchema = {
name: ‘ThingJS’,
properties: {
What: ‘string’,
Where: ‘string’,
}
};

const config = {
schema: [{ name: ‘ThingJS’, properties: {What: ‘string’, Where: ‘string’} }],
sync: {
user: ‘…’,
url: ‘…’,
error: err => console.log(err)
}
};

Realm.open({schema: [config]})
.then(realm => {
// Create Realm objects and write to local storage
realm.write(() => {
const mouse= realm.create(‘Thing’, {
What: ‘mouse’,
Where: ‘table’,
});

  const glasses= realm.create('Thing', {
    What: 'glasses',
    Where: 'table',
  });
});

const Things = realm.objects('Thing');
console.log('No of Things '+Things.length);

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

Runnig this, I get this error message (from node.js):

            let promise = Promise.resolve(new realmConstructor(config));
                                          ^

Error: Failed to read ObjectSchema: name must be of type ‘string’, got (undefined)
at Function.open (c:\Code\Tingle\JS\node_modules\realm\lib\extensions.js:69:47)

What is it that I have misunderstood?


#2

I think your issue is where you’re opening the realm, you’re passing in an array (with the whole config object as the sole member) as the schema as the argument to Realm.open(). If you just want to work locally rather than with the synced realm, tryRealm.open({schema: config.schema}).then(realm =>{...) instead.

If you want to open the synced realm, so long as the user and url props of the config object are valid, you can just do Realm.open(config).then(realm => {... :slight_smile: