2

I am very new to "Realm" and I can't figure out what's wrong with my code. I am just trying to write a simple set of data to my realm that I have already created in the realm-studio. Here is my code;

var Realm = require('realm');
var constants = require('./constants');


busSchema = {
  name: 'bus',
  properties: {
    make: 'string',
    model: 'string',
    miles: 'int'
  }
}

carSchema = {
  name: 'car',
  properties: {
    name: 'string'
  }
}



const errorCallback = function errorCallback(error) {
  console.log(error.name, error.message);
}



async function main() {

  const credentials = Realm.Sync.Credentials.usernamePassword(constants.username, constants.password, false);
  const adminUser = await Realm.Sync.User.login(constants.httpUrl, credentials);
  Realm.open({
    schema: [busSchema, carSchema],
    sync: {
      error: errorCallback,
      user: adminUser,
      url: `${constants.realmUrl}/latency_test`,
    }
  }).then(realm => {
    console.log('writing...')
    realm.write(() => {
      realm.create('bus', {
        make: 'Honda',
        model: 'Civic',
        miles: 1000,
      });
    });
  })
}

main()

My constants.js file looks like this;

module.exports = {
    username: "user",
    password: "password",
    realmUrl: "realms://myinstance.cloud.realm.io",
    httpUrl: "https://myinstance.cloud.realm.io",
};

But the script doesn't write anything on the realm that I have created in the realm-studio.

It just prints out the following;

PS D:\Projects\node\database_latency> node .\realm\realm_listener.js
Connection[1]: Session[1]: client_reset_config = true, Realm exists = false, async open = true, client reset = false
Connection[1]: Session[1]: Client reset config, metadata_dir = 'D:\Projects\node\database_latency\realm-object-server\86735845-392f-497a-84b1-1c68c91b7c80\realms%3A%2F%2Fmyinstance.cloud.realm.io%2Flatency_test.resync', recover_local_changes = true, require_recent_state_realm = true
Connection[1]: Connected to endpoint '34.214.109.102:443' (from '127.0.0.1:60367')

The log in my realm studio is as follows;

100.96.24.39 - POST /auth HTTP/1.1 200 940 - 66.268 ms [provider: password, username: 'user', register: false]
127.0.0.1 - GET /realms/files/%2Flatency_test?shouldCreate=true&realmType=full HTTP/1.1 200 79 - 0.710 ms
100.96.11.36 - POST /auth HTTP/1.1 200 1037 - 3.835 ms [provider: realm, path: '/latency_test']
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":15,"unknown":8,"total":72}
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":13,"unknown":8,"total":70}
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":14,"unknown":8,"total":71}
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":13,"unknown":8,"total":70}
127.0.0.1 - GET /health?thisInstance=true HTTP/1.1 200 20 - 0.518 ms
100.96.5.4 - GET /health HTTP/1.1 200 20 - 2.006 ms
100.96.11.36 - POST /auth HTTP/1.1 200 940 - 2.261 ms [provider: jwt/central-admin]
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":14,"unknown":8,"total":71}
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":13,"unknown":8,"total":70}
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":14,"unknown":8,"total":71}
Open files in Realm Object Server: {"regular":27,"directory":0,"character-device":1,"block-device":0,"pipe":21,"symbolic-link":0,"socket":13,"unknown":8,"total":70}
127.0.0.1 - GET /health?thisInstance=true HTTP/1.1 200 20 - 0.500 ms
100.96.24.39 - GET /health HTTP/1.1 200 20 - 2.381 ms
100.96.24.39 - POST /auth HTTP/1.1 200 940 - 3.422 ms [provider: jwt/central-admin]

Please Help!!!

  • How long are you allowing the program to run for before terminating? Do you wait until the console gives `Process finished with exit code 0`? For me in the past, it has taken Realm Object Server well over 30 seconds to do a simple write procedure. – elight Mar 03 '20 at 06:41
  • The process just quits after saying `connected to endpoint`, It just terminates without any error code – Kuldip Bhutoria Mar 03 '20 at 06:50
  • If you can, share the project via github and I could take a look in the morning (it is after 11pm in my timezone :) – elight Mar 03 '20 at 07:14
  • sure what is your github ID? – Kuldip Bhutoria Mar 03 '20 at 07:26
  • here is the link to the repository https://github.com/Kuldip-Bhutoria/realm – Kuldip Bhutoria Mar 03 '20 at 07:48
  • When I built and ran the project with my own server urls and credentials, it ran fine, creating a car object as intended. Try to find any other evidence of error output from the system. Note that the only difference between your code and mine would be server url and credential string constants. But if those were "off" you should get clear feedback from the system. – elight Mar 03 '20 at 15:55
  • Did it actually add the data to the realm and you could see it in realm studio? – Kuldip Bhutoria Mar 04 '20 at 02:47
  • Affirmative, yes – elight Mar 04 '20 at 04:38
  • what version of node and realm are you using? – Kuldip Bhutoria Mar 04 '20 at 04:43
  • Node 8.9.4 and whatever realm version you specified in your `package.json`. I don't think versions are the culprit here. Did you check my remark about server url strings and credentials? I'd rule those out first. – elight Mar 04 '20 at 05:00
  • turns out I was just missing a boolean property`update` in the `realm.create` method. That just made it work and I don't even know how – Kuldip Bhutoria Mar 04 '20 at 05:51

0 Answers0