question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get Object Returns Realm Object Instead of Class Instance

See original GitHub issue

I have a model like this, to be able to store details field as JSON

class Person  {
  set details(data) { this._details = data ? JSON.stringify(data) : null; }
  get details() { return this._details ? JSON.parse(this._details) : null; }
}

Person.schema = {
  name: 'Person',
  primaryKey: 'id',
  properties: {
    id: 'string',
    name: 'string',
    _details: { type: 'string', optional: true }
  }
};

Now when I do

realm.write(() => {
  // this is a write block for some other objects, but at some point I need to find a person
  let person = realm.objectForPrimaryKey('Person', id);
  console.log(person);
});

It will log out an instance of RealmObject with getters and setters for _details but nothing for details, so its not an instance of Person, am I missing something here?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

16reactions
alexsotocxcommented, Oct 4, 2016

I made that working by doing something like


export default class RealmModel {
  static types = {
    bool: true,
    string: true,
    int: true,
    boolean: true,
    float: true,
    double: true,
    date: true
  }

  toString() {
    return JSON.stringify(this.toJSON());
  }

  toJSON() {
    let schema = this.constructor.schema.properties;
    let json = {};
    for (var varId in schema) {
      if (schema.hasOwnProperty(varId)) {
        let type = null;
        if(schema[varId] instanceof Object){
          type = schema[varId]['type'];
        } else {
          type = schema[varId];
        }

        if(RealmModel.types[type]) {
          json[varId] = this[varId];
        } else if(type === 'list') {
          let obj = this[varId];
          json[varId] = [];
          for(let i = 0; i < obj.length; i++){
            json[varId].push(obj[i].toJSON());
          }
        } else {
          json[varId] = this[varId].toJSON();
        }
      }
    }
    return json;
  }
}

And then in the class definition

import RealmModel from './RealmModel';
class EventType extends RealmModel {

}

EventType.schema = {
  name: 'EventType',
  primaryKey: 'id',
  properties: {
    id: 'string',
    name: 'string',
    description: {type: 'string', optional: true},
    eventCategories: {type: 'list', objectType: 'EventCategory', default: []}
  }
};

export default EventType;

I just use eventType.toJSON() and I get

{ id: '1',
      name: 'Visita',
      description: 'Visitas a la obra',
      eventCategories: 
       [ { id: '1', name: 'Tecnicas', description: 'Visitas tecnicas' },
         { id: '2', name: 'Filial', description: 'Visitas familiares' } ] }
0reactions
yuvaraj119commented, Jul 5, 2018

@alexsotocx how to check the types for int?[ ] or string?[ ] …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Realm Query in React Native Returns Realm Object Instead of ...
It will log out an instance of RealmObject with getters and setters for _details but nothing for details , so its not an...
Read more >
Object Class Reference - MongoDB
Object is a class used to define Realm model objects. In Realm you define your model classes by subclassing Object and adding properties...
Read more >
Factory.Realm - IBM
Retrieves an object of the Realm class by the specified connection and symbolic name. This method always makes a round-trip to the server....
Read more >
instanceof - JavaScript - MDN Web Docs
// This class allows plain objects to be disguised as this class's instance, // as long as the object has a particular flag...
Read more >
Class Realm
This method modifies the object in-place, meaning that after it has run, ... the returned object will be an instance of a user-defined...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found