Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
How to set a variable in firebase realtime-database rules

So my database looks like this:

enter image description here

And my rules look like this:

{
  "rules": {
    ".read": true,
    ".write": true,
    "_default_user":{            <---- this is what I need to change
      "cart":{
        ".indexOn": "product/id"
      }
    }
  }
}

This is my code for removing a product from cart:

removeFromCart(itemRemoved){

    let account = JSON.parse(localStorage.getItem('user')) === null ? '' : JSON.parse(localStorage.getItem('user')).email
    account == '' ?  account = ['_default', ''] : account = /([^@]+)/.exec(account)

    const cartRef = firebase.database().ref('/' + account[0] + '_user' + '/cart');
                               // Do I need to change this ^ as well?

    const itemQuery = cartRef.orderByChild("product/id").equalTo(itemRemoved.id);

    let itemRef:any = ''
    itemQuery.get().then((snapshot) => {
       snapshot.forEach((productSnapshot) => {
          itemRef = firebase.database().ref('/' + account[0] + '_user' + '/cart' + '/' + productSnapshot.key);
       })
    itemRef.remove()
    })
}

For now, to remove an item from cart I need to manually add accounts to my rules, which of course, is not feasible. I basically need to have indexOn on all possible accounts without actually doing all that manually.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Turn out I just need to do this:

    {
      "rules": {
        ".read": true,
        ".write": true,
        "$user":{                 // <---
          "cart":{
            ".indexOn": "product/id"
          }
        }
      }
    }

Just type in the variable and .indexOn will apply to every node in root( in my case )

https://firebase.google.com/docs/database/security/core-syntax

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!