question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Objects method returns nothing

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
steffenaggercommented, Oct 8, 2020

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.

4reactions
steffenaggercommented, Oct 8, 2020

@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())
Read more comments on GitHub >

github_iconTop Results From Across the Web

Should functions return null or an empty object? - Stack Overflow
An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned. Additionally, returning a null will ......
Read more >
Stop Returning Null in Java - Code by Amir | Amir Boroumand
In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. Calling...
Read more >
Is it better to return NULL or empty values from functions ...
An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned. Additionally, returning ...
Read more >
Return Optional not null - Java Practices
Returning a possibly- null object from a method is usually undesirable. The caller of such a method may not be aware that it...
Read more >
Returning a Value from a Method (The Java™ Tutorials ...
Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found