Realm-js: Objects method returns nothing

Created on 7 Oct 2020  路  5Comments  路  Source: realm/realm-js

I'm developing a Music App and now I'm implementing the offline part of the app. I decided to use RealmDB, but I'm having a hard time with it. I previously created another issue (https://github.com/realm/realm-js/issues/3304) that was motivated by this problem. When I call this.realm.objects(whatever) I get nothing in return. Code samples show better what's happening.

Goals

Implement RealmDB

Expected Results

Use query successfully

Actual Results

Nothing is returned

Steps to Reproduce

call realm.objects method

Code Sample

This is part of the class that is responsible for managing Realm:

const SCHEMA_VERSION = 17

class AppDatabase {
    realm: Realm

    constructor() {
        console.log('Creating Realm instance: ')

        this.realm = new Realm({
            schema: [Playlist, Song], 
            schemaVersion: SCHEMA_VERSION,
        })

        console.log(this.realm.objects('Playlist')) // Doesn't get logged
        console.log('Realm Instance Created')
    }

   /* Class methods */
}

const AppDatabaseService = new AppDatabase()

export default AppDatabaseService

That's the console log:
image

I also tried instantiated and using Realm directly on a component just to make sure.

UserPlaylist component:

const UserPlaylists: React.FC<UserPlaylistsProps> = ({
    isFocused,
    isCreatingPlaylist,
    disableIsCreatingPlaylist,
}) => {
    const realm = new Realm({
        schema: [PlaylistSchema, SongSchema],
        schemaVersion: 17,
    })

   /* ... */

    useEffect(() => {
        isFocused && getPlaylists()
        console.log(realm.objects('Playlist')) // Doesn't get logged
    }, [isFocused])

    /* ... */

But the result is the same, nothing is logged. I also tried using async/await but nothing changed.
As I'm fairly new using Realm, I'm sorry if I'm missing something stupid that's causing this kind of behavior.
Thank you in advance.

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

No problem @gustavo-dev & thanks for the feedback, you highlighted an issue we'll have to look at 馃憤

Btw (I forgot this in my previous post), an added "bonus" of fetching on the Class Model type (Playlist), and not a string, is that the results returned will all be of the type Playlist, so any instance-functions etc. will be available on the individual objects.

All 5 comments

Please try to use console.log(realm.objects('Playlist').length) or console.log(realm.objects('Playlist').toJSON()).

length returns 1 and toJSON() throws an error
image

@gustavo-dev that looks like we have a bug when mixing Class Models with a string-based fetch (never should throw...).

But could you just try this:

Ensure your Class Models extends Realm.Object:

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},
        },
    }
}

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',
            isSlowed: 'bool',
        },
    }
}

Define schema:

this.realm = new Realm({
  schema: [Playlist, Song],
  schemaVersion: SCHEMA_VERSION,
})

And then fetch with:

console.log(realm.objects(Playlist).toJSON())

instead of:

console.log(realm.objects('Playlist').toJSON())

Hi @steffenagger, thank you for your response! I tried your solution and it works now.
image

I'll close this issue. Thank you @steffenagger and @kneth for helping me.

No problem @gustavo-dev & thanks for the feedback, you highlighted an issue we'll have to look at 馃憤

Btw (I forgot this in my previous post), an added "bonus" of fetching on the Class Model type (Playlist), and not a string, is that the results returned will all be of the type Playlist, so any instance-functions etc. will be available on the individual objects.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CrystalRanita picture CrystalRanita  路  3Comments

fever324 picture fever324  路  4Comments

matt2legit picture matt2legit  路  3Comments

emrehayirci picture emrehayirci  路  3Comments

laznrbfe picture laznrbfe  路  3Comments