在 Cloud Firestore 安全规则中将字符串转换为 Int

cre*_*not 3 firebase firebase-security google-cloud-firestore

我就是从这个问题来的。我想过开始对它进行赏金,因为它已经几个月没有受到任何关注,而且随着时间的推移我没有看到任何答案,但我认为我的愿望有点不同。

如果有一个数字作为文档的字段,但指定为 a string

截屏

Firestore 规则出现问题。

像这样的检查变得不可能:

resource.data.number > 11
Run Code Online (Sandbox Code Playgroud)

不可能像比较int那样比较字符串

模拟器异常如下:

Unsupported operation error. Received: string > int. Expected: constraint > constraint, int > int, float > float, duration > duration, timestamp > timestamp, constraint > any, any > constraint. 
Run Code Online (Sandbox Code Playgroud)

有没有办法将我的操作转换为int > int

Jer*_*myW 5

查看此链接,该链接向您展示了如何在 Firestore 规则文档中将字符串转换为整数。

int("2") == 2
int(2.0) == 2
Run Code Online (Sandbox Code Playgroud)

所以你的支票可能是:

int(resource.data.number) > 11
Run Code Online (Sandbox Code Playgroud)