Realm-js: to-many Relationship returning Circular object when ids are them same

Created on 10 Oct 2020  Â·  2Comments  Â·  Source: realm/realm-js

I currently have two schemas on my app:

Playlist

export default class Playlist extends Realm.Object {
    static schema = {
        name: 'Playlist',
        primaryKey: 'id',
        properties: {
            id: {type: 'int', indexed: true},
            title: 'string',
            songs: 'Song[]',
            author: 'string',
            isDownloaded: {type: 'bool', default: false},
        },
    }
}

Song

export default class Song extends Realm.Object {
    static schema = {
        name: 'Song',
        primaryKey: 'id',
        properties: {
            id: {type: 'int', indexed: true},
            title: 'string',
            url: 'string',
            artwork: 'string',
            artist: 'string',
            stringId: 'string',
            slowed: 'bool',
        },
    }
}

Goals

Display a Playlist's songs

Expected Results

Songs being displayed correctly

Actual Results

Song with the same id from the current Playlist is returned as Circular Object, that returns the playlist itself when accessed, after calling toJSON()

image

When I console log this object before calling toJSON(), the Song is shown correctly
When I console log this object after I called this.realm(Playlist).toJSON(), it returns the playlist itself

// '8' is the index of this Circular Object
console.log(realm.objects(Playlist)[0].songs[8]) // Works fine

console.log(realm.objects(Playlist).toJSON()[0].songs[8]) // Returns the `Playlist` itself, instead of the `Song`

Steps to Reproduce

Create a Playlist containing an id equals to one of it's Songs.
Call toJSON()

Code Sample

const playlist = realm.objects(Playlist).toJSON()[0]
console.log(playlist)

Version of Realm and Tooling

  • Realm JS SDK Version: 6.1.3
  • Node or React Native: 4.12.0
  • Client OS & Version: Win 10
  • Which debugger for React Native: None
O-Community

Most helpful comment

@gustavo-dev just a quick note — you don't have to write indexed to id property, since it's a primary key and indexed by default :)

All 2 comments

Error now located, expect a fix in the next release.

@gustavo-dev just a quick note — you don't have to write indexed to id property, since it's a primary key and indexed by default :)

Was this page helpful?
0 / 5 - 0 ratings