[英]how to return data through function when fetching data from firebase realtime-database in node.js
[英]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.