-3

I have tried to use .indexOn of firebase on field 'status' which is inside the child of 'BOUGHT_PRODUCT'.

But, when i run following query.

db.ref().child('BOUGHT_PRODUCT')
.orderByChild('status')
.equalTo('Service')
.on("value", function (snapshot) {
            console.log(snapshot.val());
        });

I get null snapshots. Even, Firebase warning of index : "@firebase/database: FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "status" at /BOUGHT_PRODUCT to your security rules for better performance."

Firebase Data: Firebase Data

Index firebase: Index firebase

Your help would be appreciated.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

Firebase Database queries run against each child under the location where you run them. The field you order/filter on must be at a fixed path under each child. Since you run the query on /BOUGHT_PRODUCT, the database searches for /BOUGHT_PRODUCT/$uid/status. And since that property doesn't exist, there are no results matching the query.

In other words: your current data structure allows you to query a specific user for the status of their products, but not across all users. If you want to implement that use-case, you will need to create a data model that allows it, e.g. a single top-level list of product statuses.

Statuses
  product1: "Service"
  product2: "Service"
  product3: "Something else"

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807