-1

I want to orderby shopId but it returns:

"error" : "Index not defined, add ".indexOn": "shopId", for path "/Products", to the rules".

Would appreciate any help thanks!

data

Rules :

{
  "rules": {
    ".read": "now < 1609966800000",  // 2021-1-7
    ".write": "now < 1609966800000",  // 2021-1-7
      "Products":{
        "Records":{
          ".indexOn":["shopId"]
        }
      }
  }
}

Pyrebase code:

result = db.child("Products").order_by_child("shopId").equal_to(shopId).get()
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
alig
  • 1
  • 1

1 Answers1

0

Firebase Realtime Database can only filter the direct child nodes on which you run the query. The value which you order/filter on must be at a fixed path under each direct child node.

In your data structure you seem to have two unknown keys: UID and unique_recordid. This means you cannot query for shopId from Products.

You can query the records for a single user:

db.child("Products").child("UID of a specific").child("Records")
  .orderByChild("shopId").equalTo("value of shopId")

But you can't do the same query on all of Products as the path of the shopId property is not the same for all child nodes of Products.

Also see Firebase Query Double Nested


If you want to query for records with a specific shopId, you'll need in your database a flat list of exactly those nodes.

Records: {
  "recordid1" : {
    uid: "...",
    productId: "...".
    ...
  },
  "recordid1" : {
    uid: "...",
    productId: "...".
    ...
  }
}

On this data you can then query for productId and look up the other data, including the UID that is now duplicated for each record.

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