Realm-js: Realm returning proxy object when realm.objects is called

Created on 13 Jan 2018  路  6Comments  路  Source: realm/realm-js

I am using realm 2.1.1, react-native 0.51.0 and running debug code on Android emulator.
Issue is that when I do let item = realm.objects('mySchema') proxy object is returned.
Please note that I filter after.
I can get array object by using itemArray = Array.from(item) but I have failed to update object within the write transaction.
When I do so nothing happens

  realm.write(() => {
       itemArray[0] = newItem 
       //new item is object created using a class 
       //with the constrator. It works well in 
       //realm.create()
  });

Version of Realm and Tooling

  • Realm JS SDK Version: 2.1.1
  • Node or React Native: 6.11.4 or 0.51.0
  • Client OS & Version: Windows 10
O-Community T-Help

Most helpful comment

Thanks for the response. It's true I create the realm objects using Realm.create as specified in the docs. Say for example

//code sample_1
realm.write(() => {
  realm.create('Person', {
    name: 'Joe',
    // nested objects are created recursively
    car: {make: 'Honda', model: 'Accord', drive: 'awd'},
  });
});

Say I want to change the make of Joe's car. I query the database to give me a Joe object as follows

//code sample_2
let Joe = realm.objects('Person').filtered('name= "Joe"');

And then I can update Joe's car make by

//code sample_3
realm.write(() => {
  Joe.car.make= 'Isuzu';
});

My challenge is, code sample_2 returns a proxy object, and therefore I cannot run code sample_3 as it is given in that example.

One more thing you should note, is that this is the second project where I am using Realm db, with the first one everything worked as expected, as stipulated in the docs. The Realm version was 2.0.3.

This second one with Realm 2.1.1 has got me scratching my head. Any help will highly be appreciated.

All 6 comments

Realm is managing objects created by Realm.create. This implies that a Realm object is not an ordinary JavaScript object (we go as far as possible to keep up the illusion). But you will have to create object using Realm.create.

Thanks for the response. It's true I create the realm objects using Realm.create as specified in the docs. Say for example

//code sample_1
realm.write(() => {
  realm.create('Person', {
    name: 'Joe',
    // nested objects are created recursively
    car: {make: 'Honda', model: 'Accord', drive: 'awd'},
  });
});

Say I want to change the make of Joe's car. I query the database to give me a Joe object as follows

//code sample_2
let Joe = realm.objects('Person').filtered('name= "Joe"');

And then I can update Joe's car make by

//code sample_3
realm.write(() => {
  Joe.car.make= 'Isuzu';
});

My challenge is, code sample_2 returns a proxy object, and therefore I cannot run code sample_3 as it is given in that example.

One more thing you should note, is that this is the second project where I am using Realm db, with the first one everything worked as expected, as stipulated in the docs. The Realm version was 2.0.3.

This second one with Realm 2.1.1 has got me scratching my head. Any help will highly be appreciated.

filtered returns a collection of objects matching the given predicate. Your code snippets attempt to use that collection as though it鈥檚 a single object, which won鈥檛 work. You need to retrieve the relevant object from the collection in order to update it.

This appears to be resolved.

Hi guy, my solution was return a function with instance inside Schema:

//Schema
export default class UserSchema {
  static schema = {
    name: 'Users',
    primaryKey: 'id',
    properties: {
      id: {type: 'int', indexed: true},
      name: 'string',
      cr: 'int',
      data_nascimento: 'int',
      ghe: 'string',
      path: 'string',
      epis: 'Epis[]',
    },
  };

  get values() {
    return {
      id: this.id,
      name: this.name,
      cr: this.cr,
      data_nascimento: this.data_nascimento,
      ghe: this.ghe,
      path: this.path,
      epis: this.epis.map(e => e.values),
    };
  }
}

//call
const realm = await getRealm();
const response = realm.objects('Users').sorted('name');
console.tron.log(response.map(user => user.values));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

CrystalRanita picture CrystalRanita  路  3Comments

MihaelIsaev picture MihaelIsaev  路  3Comments

kontinuity picture kontinuity  路  3Comments

gwanjar picture gwanjar  路  3Comments

fever324 picture fever324  路  4Comments