/    Sign up×
Community /Pin to ProfileBookmark

Firebase – How to use orderByChild!?

I’m trying to use orderByChild to sort quizzes by newest or most played for my game. I tried:

“`javascript
var mostViewedPosts = firebase.database().ref(‘quizzes’).orderByChild(‘plays’);

console.log(mostViewedPosts)
“`

and it logs out some random thing I don’t understand at all, and it doesn’t have any documentation as far as I could find. How do you use orderByChild!? Like how do I get the data ordered?? Thanks

EDIT: Just tried this:

“`javascript
var mostViewedPosts = firebase.database().ref(‘quizzes’).orderByChild(‘plays’);

mostViewedPosts.get().then(data => {
console.log(data.val())
})
“`

and I get this error:
`Uncaught (in promise) Error: Index not defined, add “.indexOn”: “plays”, for path “/quizzes”, to the rules at Repo.ts:472:29`

Please help!

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 17.2022 — @Bost#1644724 No clue of firebase, however IMO the error message says it all: Obviously ordering is possible only when there is an index on the column ("plays") in question. Try to create it as described here:

https://firebase.google.com/docs/firestore/query-data/indexing?hl=en
Copy linkTweet thisAlerts:
@BostauthorJun 17.2022 — @Sempervivum#1644726

> @Sempervivum#1644726 https://firebase.google.com/docs/firestore/query-data/indexing?hl=en

Sorry, I'm not using Firestore, I'm using RTDB.
Copy linkTweet thisAlerts:
@BostauthorJun 17.2022 — @Sempervivum#1644726 Do I need to do var mostViewedPosts = firebase.database().ref('quizzes').indexOf('plays') or something?

Database structure:

  • - Each quiz has a ten digit number as the key. (The reference to a quiz would be something like "quizzes/1234567890")

  • - Each quiz has questions inside of it.

  • - Each quiz has a quiz name, the epoch time it was created (for newest sorting), and of course, the amount of times it was played.


  • Not sure if this is why, but there's currently only one quiz with 0 plays.
    Copy linkTweet thisAlerts:
    @SempervivumJun 17.2022 — Check if this helps you:

    https://stackoverflow.com/questions/34968413/error-index-not-defined-add-indexon
    Copy linkTweet thisAlerts:
    @BostauthorJun 18.2022 — @Sempervivum#1644735 Ignore that this may be a bad practice, but my project doesn't have any rule restrictions at all... it's just ".read": true and ".write": true.
    Copy linkTweet thisAlerts:
    @BostauthorJun 20.2022 — Bump
    Copy linkTweet thisAlerts:
    @BostauthorJun 22.2022 — Can someone please help me with this :/
    Copy linkTweet thisAlerts:
    @BostauthorJun 22.2022 — I finally fixed the error! But now, it just doesn't sort them at all. I made one quiz have 1 play and the other have 0 plays, but it still put the one with 0 first!
    Copy linkTweet thisAlerts:
    @BostauthorJun 22.2022 — I fixed it 👍️
    Copy linkTweet thisAlerts:
    @BostauthorJun 22.2022 — How do I put it in reverse??
    Copy linkTweet thisAlerts:
    @BostauthorNov 07.2022 — @Bost#1644782 Resolved: I got the sorted array of items, threw on a .reverse on it, and plugged it back into a foreach statement.
    ×

    Success!

    Help @Bost spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 3.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,

    tipper: Anonymous,
    tipped: article
    amount: 10 SATS,
    )...