firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Indexes are not required for development unless you are using the REST API. - error using WebSocketConnection class

Open tonyhallett opened this issue 1 year ago • 3 comments

Operating System

Windows 11

Environment (if applicable)

Version 128.0.6613.115 (Official Build) (64-bit)

Firebase SDK Version

^10.12.2

Firebase SDK Product(s)

Database

Project Tooling

React, typescript, parcel

Detailed Problem Description

I am iterating over a realtime database schema.
According to https://firebase.google.com/docs/database/security/indexing-data

Firebase allows you to do ad-hoc queries on your data using an arbitrary child key. If you know in advance what your indexes will be, you can define them via the .indexOn rule in your Firebase Realtime Database Security Rules to improve query performance.

Indexes are not required for development unless you are using the REST API.

So why am I receiving Index not defined, add ".indexOn": "pNumber", for path "/things", to the rules when the getOrderedByNumber callback is executed ?

Note that this also occurs with the emulator attached.

Steps and code to reproduce issue

export function DemoFiltering() {
  const rtb = useRTB();
  const thingsListRef = ref(rtb, "things");
  const orderByNumber = orderByChild("pNumber");
  const getOrderedByNumber = useCallback(
    async (constraint: QueryConstraint | undefined, info: string) => {
      let snapshot: DataSnapshot;
      let orderByNumberQuery: Query;
      try {
        if (constraint) {
          orderByNumberQuery = query(thingsListRef, constraint, orderByNumber);
        } else {
          orderByNumberQuery = query(thingsListRef, orderByNumber);
        }
        snapshot = await get(orderByNumberQuery);
      } catch (reason) {
          // `Index not defined, add ".indexOn": "pNumber", for path "/things", to the rules`
      }
   //.other code

tonyhallett avatar Sep 07 '24 13:09 tonyhallett

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Sep 07 '24 13:09 google-oss-bot

Thanks for submitting this issue @tonyhallett.

I found a commit that might have made orderByChild require an index to be defined: https://github.com/firebase/firebase-js-sdk/commit/2a5039ee3242fb4109da9dee36ac978d78519334. This was introduced way back in 8.6.3.

dlarocque avatar Sep 13 '24 15:09 dlarocque

I don't think that is related. This code is after the server has responded. A quick search https://github.com/search?q=repo%3Afirebase%2Ffirebase-js-sdk+%22Index+not+defined%22&type=code has no code results of "Index not defined". Of course this could be created by an expression.

I will debug the server code to be sure that the error has come from there. If it has the question is why is it throwing when the docs state that we may index.

  return repo.server_.get(query).then(
    payload => {
      const node = nodeFromJSON(payload).withIndex(
        query._queryParams.getIndex()
      );

tonyhallett avatar Sep 13 '24 19:09 tonyhallett