Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase no index defined even when using .indexOn correctly

Tags:

firebase

I've attempted to work with the Firebase (2.2.5) API / .orderByChild('score') operation and I keep getting

No index defined for score

Although documentation of Firebase states I don't need to create an index for score when using the WebSocket API, I did create it. Markup:

Rules

{
"rules": {
    ".read": false,
    ".write": false,

    /* index players by score */
    "Players": {
      ".read": true,
      ".indexOn": "score",
      "$playerID": {
        ".write": "!data.exists() || data.child('uid').val() === auth.uid"
      }
    }
  }
}

Data

{
    "Players": {

      "-JpqOuoaHmcA2EngEvSZ" : {
        ".priority" : 0.0,
        "avatar" : "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRS9Yh2lWzGP1oojIwNVV2JQUZYFX4_4pxvaADz8TfDF_WGrHbs",
        "draw" : 9,
        "lastTimeAccessed" : 1432705280027,
        "lost" : 7,
        "name" : "Hans",
        "score" : 126,
        "won" : 36
      },

      "-JpzVTpXZUZG-V0qJCDT" : {
        ".priority" : 2.0,
        "avatar" : "http://static.wixstatic.com/media/6a9a03_f979928a006c4d2ebc179c1c627dc119.png_srz_300_300_75_22_0.50_1.20_0.00_png_srz",
        "draw" : 0,
        "lastTimeAccessed" : 1432705280532,
        "lost" : 25,
        "name" : "Safari",
        "score" : 9,
        "won" : 3
      }
    }
}

Calling

I have a Firebase() object pointed at http://theapp.firebaseio.com/Players for which I am calling orderByChild('score'). I would expect this to work.

Looking forward to some fresh views or tips.

like image 335
H. van den Akker Avatar asked Nov 09 '22 12:11

H. van den Akker


1 Answers

I ran into this error when using this code:

const snap = await admin
    .database()
    .ref("keepAlive")
    .orderByChild("lastSeen")
    .get();

  snap.forEach((result) => {
    console.log({ result });
  });

Changing the code to this makes it work:

const snap = await admin
    .database()
    .ref("keepAlive")
    .orderByChild("lastSeen")
    .once("value");

  snap.forEach((result) => {
    console.log({ result: result.val() });
  });
like image 129
Miguel Coxo Avatar answered Dec 27 '22 21:12

Miguel Coxo