Home > Mobile >  Firebase indexOn for a nested property
Firebase indexOn for a nested property

Time:03-07

My database structure is this:

 {
   "Products" : {
     "0100251633" : {
       "codes" : {
         "Call" : "000156",
         "EAN13" : "7898613211028"
       },
       "productid" : "0100251633",
       "nmproduct" : "BRW 12 COLOR HYDROGRAPHIC PEN",
       "quantity" : 0,
       "stativo" : "S",
       "url" : "SemUrl"
     }
   }
 }

I need to sort(index) my database by the "Call" code.

The rule that works (ordered by description) is this one:

 {
   "rules": {
     ".read": "auth.uid != null",
     ".write": "auth.uid != null",
       "Products":{
         ".indexOn": ["nmproduct","codes","Call"]
       }
   }
 }

But I need to sort by calling code. I'm trying this way because this is how I understand it:

 {
   "rules": {
     ".read": "auth.uid != null",
     ".write": "auth.uid != null",
       "Products":{
         "Codes":{
             ".indexOn": ["Call"]
         }
       }
   }
 }

It's not working, I'm getting the return below:

 raise HTTPError(e, request_object.text)
 requests.exceptions.HTTPError: [Errno 400 Client Error: Bad Request for url:           https://inventarioshop-8318f-default-rtdb.firebaseio.com/Produtos.json?     auth=65465465465465465465465465&orderBy="Chamada"] {
    "error" : "Index not defined, add \".indexOn\": \"Call\", for path \"/Products\", to the rules"
  }

I would really appreciate your help to understand and be able to explain to other people as well.

CodePudding user response:

To index a nested property under a node, you need to specify the path to that property in the index definition.

So for your call code that'd be:

"Products":{
  ".indexOn": ["nmproduct","codes/Call"]
}
  • Related