簡體   English   中英

如何在 firebase 實時數據庫規則中設置變量

[英]How to set a variable in firebase realtime-database rules

所以我的數據庫看起來像這樣:

在此處輸入圖像描述

我的規則如下所示:

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

這是我從購物車中刪除產品的代碼:

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()
    })
}

現在,要從購物車中刪除一個項目,我需要手動將帳戶添加到我的規則中,這當然是不可行的。 我基本上需要在所有可能的帳戶上都有 indexOn 而無需手動執行所有這些操作。

原來我只需要這樣做:

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

只需輸入變量 and.indexOn 將應用於根中的每個節點(在我的情況下)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM