Objects method returns nothing
See original GitHub issueI’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:
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
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
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.@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
:Define schema:
And then fetch with:
instead of: