我需要删除request.auth.phone_number返回的字符串的加号信号。为此,我尝试使用替换功能,但我收到以下错误:“找不到功能错误:名称:[替换]。”;
match /test/{id} {
allow read, update, delete, create: if request.auth != null && (resource.data.items[request.auth.token.phone_number.replace('+', '')] == true || resource == null);
}
当我在Realtime Database运行时,这很好用。例如:
"tests": {
"$uid": {
".write": "auth.uid.replace('+', '') === '5521999991234'"
}
}
有没有办法在Cloud Firestore数据库中使用字符串函数,如“contains(),replace(),toLowerCase()”等等?
谢谢
你可以写一个这样的函数:
function replace(string, replace, by) {
return string.split(replace).join(by)
}
并使用它..
replace('+3312312345', '+', '')
在你的例子中:
match /test/{id} {
allow read, write: if request.auth != null &&
(resource.data.items[replace(request.auth.token.phone_number, '+', '')]
== true || resource == null);
}
// below your rules
function replace(string, replace, by) {
return string.split(replace).join(by)
}
Firestore安全规则使用与实时数据库完全不同的语言。
您可以在API documentation中查看字符串对象上可用的所有方法的列表。你唯一要做的就是lower和matches。