如何在 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"
}
}
}
}
只需输入变量,.indexOn 将应用于根目录中的每个节点(在我的例子中)
https://firebase.google.com/docs/database/security/core-syntax
所以我的数据库是这样的:
我的规则是这样的:
{
"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"
}
}
}
}
只需输入变量,.indexOn 将应用于根目录中的每个节点(在我的例子中)
https://firebase.google.com/docs/database/security/core-syntax