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.
Implement RealmDB
Use query successfully
Nothing is returned
call realm.objects method
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:

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.
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

@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.

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.
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 astring, is that the results returned will all be of the typePlaylist, so any instance-functions etc. will be available on the individual objects.