RomainVialard / FirebaseApp

Google Apps Script binding for Firebase Realtime Database
Apache License 2.0
117 stars 30 forks source link

getData - optQueryParameters can't take "$key" for orderBy - "Error: 400 - Provided key index type is invalid, must be string" #28

Closed dginovker closed 2 years ago

dginovker commented 2 years ago

I was having the same issue as #23 and saw the solution to use limitToFirst & startAt for pagination as per the docs. To use startAt however, you need to use orderBy. Since my data on Firebase is in the format

{
    "hash0": "very-long-string",
    "hash1": "very-long-string",
    "hash2": "very-long-string",
    ...
}

I wanted to use orderBy="$key", however I get the following error:

image

Actual code used:

  let base = FirebaseApp.getDatabaseByUrl(url, env.token);
  return base.getData("", {orderBy: `$key`, startAt: 0, limitToFirst: 5});

Omitting the second parameter on getData() entirely returns all the data, but since I have a lot of data it fails for the same reason as other users got in #23

dginovker commented 2 years ago

For completeness, some other things I tried -

orderBy Result
orderBy: "$key" [1] Error: 400 - Provided key index type is invalid, must be string
orderBy: "$value" [2] Error: 400 - Index not defined, add ".indexOn": ".value", for path "/url/data/path", to the rules
orderBy: "$priority" [3] (Returned undefined)
orderBy: null Error: 400 - orderBy must be a valid JSON encoded path
orderBy: "generic-index"* Error: 400 - Index not defined, add ".indexOn": "generic-index", for path "/url/data/path", to the rules
orderBy: "%24key"** Error: 400 - Index not defined, add ".indexOn": "%24key", for path "/url/data/path", to the rules

* For generic-index, when I added ".indexOn": "generic-index" to the rules at the right location, it just returned undefined ** %24 is the result of Javascript's encodeURIComponent("$") so I gave that a shot

It seems like the dollar sign is being dropped so I can't index by key/value/priority properly

dginovker commented 2 years ago

Misunderstood the documentation. "$key" works,startAt just needs to reference something about the key (it is NOT a numeric index). I.e. my code needs to be something like:

  return base.getData("", {orderBy: "$key", startAt: "C"});