Realm-js: [React Native] Can't connect to Realm Server.

Created on 21 Jan 2017  路  14Comments  路  Source: realm/realm-js

Hey Guys,

I've been trying to get the preview release of 0.15.5-rmp-beta.1 working with my React Native application but running into some trouble.

Would be great to get some help on the issue and could be beneficial to others trying to get it to work.

Here are my steps so far:

  1. Downloaded the Realm Object Server for Mac.

  2. Launched start-object-server.command.

  3. Created an admin user.

  4. Tried to connect and create a Realm with the following:

    var realm;
    Realm.Sync.User.login('http://127.0.0.1:9080/', '[email protected]', 'MyPassword', (error, user) => {
      realm = new Realm({
        sync: {
          user: user,
          url: 'realm://127.0.0.1:9080/~/realm1',
        },
        schema: [MySchema],
        schemaVersion: 1
      });
    });
    

Any guidance on how to get this working would be great.

Thanks!

Josh

T-Help

Most helpful comment

Okay, let me try that and let you know how it goes. Thanks for your help with this.

All 14 comments

Hey @joshuapinter,

Do you get any error messages in the app's log output, or in the server's output?

@fealebenpae When logging in it returns null for error and an empty hash {} for user. The "realm" gets created, which can be seen in the Realm dashboard. And then my execution stops.

Realm.Sync.User's properties are defined on its prototype, so the object itself won't have any enumerable own properties. Try printing user.server or user.identity and those ought to return meaningful values.

What do you mean by "execution stops" - does the app hang or crash?

Going to try again right now and get back to you right away. Would love to get this working today.

Okay, so you're right. user.server and user.identity both return good strings:

user.server: http://lvh.me:9080/
user.identity: be67843e515dbaa4fc3137dd999b3c21

Trying to isolate the error right now.

Hmm... I think I know what the current problem might be.

Before adding the sync I was just doing:

let realm = new Realm({ ... });

module.exports = realm;

But now because the realm DB is initiated in a callback, I'm returning an undefined.

Is there a way that I can create the Realm and then add the sync on after initiating it? Or is there a better way that you can think of doing this?

Thanks!

I'm afraid you'd need to make your access to the realm asynchronous. I would export a promise that logs in and returns the realm.

Right now we don't have the ability to create a realm without a user and then, at a later time, upgrade it with a sync config.

Okay, let me try that and let you know how it goes. Thanks for your help with this.

Hey @fealebenpae. I can't seem to get any of the records to come back. After connecting to my realm with the same above code, I try and get some records but nothing:

console.log(realm); //=> {}
let relationships = realm.objects("Relationship");
console.log(relationships); //=> {}
console.log(relationships.length); //=> 0
console.log(relationships[0]); //=> undefined

However, I used the Realm Browser to connect to the Realm and add a couple of rows there.

Because of this I'm getting an error like this:
_Realm2.default.objects is not a function. (In '_Realm2.default.objects('Relationship')', '_Realm2.default.objects' is undefined)

The realm will most likely be empty immediately after you create it until it has synchronized with the server. I suggest adding a change listener:

realm.objects("Relationship").addListener((relationships, changes) => {
  console.log(`Got a Relationship change notification. Number of relationships: ${relationships.length}`);
});

How do you export the realm?
Here is how I would do it:

export default new Promise((resolve, reject) => {
  Realm.Sync.User.login('http://127.0.0.1:9080/', '[email protected]', 'MyPassword', (error, user) => {
    if (error) {
      reject(error);
      return;
    }
    let realm = new Realm({
      sync: {
        user: user,
        url: 'realm://127.0.0.1:9080/~/realm1',
      },
      schema: [MySchema],
      schemaVersion: 1
  });
  resolve(realm);
});

and to use it:

  import myRealm from './myRealm';

  myRealm.then(realm => {
    let objects = realm.objects('MySchema');
  });

Okay, I setup my Realm file and include just how you have it. Now I'm running into this error:

Cannot retrieve an existing user specifying a different auth server.

Using the same URL, username and password that is working fine on my Realm Browser.

Any ideas?

I suggest deleting your app from the device/simulator - it seems that you're trying to log in to the same server using a different address string (e.g. localhost vs 127.0.0.1) than what you've previously used.

The logged-in users are persisted to disk by default and are automatically loaded so that you don't have to hit the auth server all the time. You can find all the user objects, even the ones created in another launch of your app, in Realm.Sync.User.all.

@joshuapinter, did you resolve this?

@kristiandupont Yes I did. I decided to hold off until the official release, which was a few days ago. I then spent this morning giving it another try and it worked liked a charm.

I'll let you know if I run into trouble again but I'll close this off for now.

Many Thanks!

Was this page helpful?
0 / 5 - 0 ratings