Realm-js: sorted() method call on results object throws error when property given is a property of linked object.

Created on 26 May 2016  路  3Comments  路  Source: realm/realm-js

I'm open to ideas on a better title!

Goal

sorted results based on property of an object property.

    const people = realm.objects('Person');
    const sortedTest1 = people.sorted('identity.name'); 

Where identity is an object linked as a property on each Person object in people, and name is a string property in that Identity.

Expected Results

return results object based on the sort order correctly.

Actual Results

Red Screen of Death.
Property 'identity.name' does not exist on object type 'Person'
Trace gives the line the sorted() call is on.

Code Sample

Working (bar the bug) github project using RN 0.26.2 and Realm 0.13.1
https://github.com/Chris-Petty/realmBugTest

gist:
https://gist.github.com/Chris-Petty/2cb08fac6f1b3bc0bfced1538743c554

    realm.write(() => {
      realm.deleteAll();
      const identityOne = realm.create('Identity', {
        name: 'Bob',
        birthday: new Date(),
        address: '123 Somewhere Land',
      });

      const identityTwo = realm.create('Identity', {
        name: 'glob',
        birthday: new Date(),
        address: '9000 Somewhere Land',
      })

      realm.create('Person', {
        identity: identityOne,
        hasCar: true,
      });

      realm.create('Person', {
        identity: identityTwo,
        hasCar: false,
      });
    });

    const people = realm.objects('Person');
    const filterTest = people.filtered('identity.name CONTAINS[c] "Bob"'); // Works fine
    const sortedTest1 = people.sorted('hasCar'); // Works fine
    const sortedTest2 = people.sorted('identity.name'); // RSOD, "Property 'identity.name' does not exist on object type 'person'"
    console.log(sortedTest);

Version of Realm and tooling

Realm: 0.13.1
React Native: 0.24.1 (as in my code sample, the issue persists in 0.26, as I'd expect)

T-Duplicate

Most helpful comment

IMHO this feature MUST be come in the core. filtered() can use complex sub-objects properties.

All 3 comments

Thanks for the excellent bug report! This is not currently supported, but is being tracked in issue #437. For now, you can use Array.prototype.sort to create an Array copy of the results and do this manually. Something like this should work:

let people = realm.objects('Person').slice();
people.sort((a, b) => a.identity.name.localeCompare(b.identity.name));

Cheers. Ha probably should have searched the issues properly given that this is a duplicate.

IMHO this feature MUST be come in the core. filtered() can use complex sub-objects properties.

Was this page helpful?
0 / 5 - 0 ratings