是否可以执行If Else或Switch语句Firestore规则 [英] Is it possible to do a If Else or a Switch Statement Firestore Rules
问题描述
是否有可能在Firestore规则中执行Switch语句或其他操作?
Is it possible to do a Switch Statement or an if else in firestore rules?
我试图搜索它,但是没有找到答案的运气.
I have tried to search for it with no luck of finding an answer.
我尝试过的是
function getTier() {
return get(/users/$(request.auth.uid)).data.userTier;
}
function canAddProduct() {
if getTier() == 'UserTier.FREE'
// Do additional code
return doSomethingBasedOnFreeTier();
else if getTier() == 'UserTier.SILVER'
// Do additional code
return doSomethingBasedOnSilverTier()
else if getTier() == 'UserTier.GOLD'
// Do additional code
return doSomethingBasedOnGoldTier()
else if getTier() == 'UserTier.COMPANY'
// Do additional code
return doSomethingBasedOnCompanyTier()
}
任何帮助将不胜感激.
推荐答案
自2020年2月13日起,Firestore现在支持三元运算符
As of Feb 13, 2020 Firestore now supports the ternary operator
https://firebase.google.com/support/release -notes/security-rules#february_13_2020
以下是可用运算符的文档(三元位于底部)
Here's the documentation for the available operators (ternary is at the bottom)
https://firebase.google.com/docs/rules/rules -language#building_conditions
从问题中获取示例代码,可以通过使用嵌套的三元运算符集来实现解决方案
Taking the example code from the question, the solution can be achieved by using a nested set of ternary operators
function getTier() {
return get(/users/$(request.auth.uid)).data.userTier;
}
function canAddProduct() {
return getTier() == 'UserTier.FREE' ?
doSomethingBasedOnFreeTier() :
(
getTier() == 'UserTier.SILVER' ?
doSomethingBasedOnSilverTier() :
(
getTier() == 'UserTier.GOLD' ?
doSomethingBasedOnGoldTier() :
(
getTier() == 'UserTier.COMPANY' ?
doSomethingBasedOnCompanyTier() :
null
)
)
)
}
这篇关于是否可以执行If Else或Switch语句Firestore规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!