Firestore规则:当数据为数字时,条件检查失败 [英] Firestore rules: condition check fails when data is number

查看:46
本文介绍了Firestore规则:当数据为数字时,条件检查失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个示例规则,当条件中使用的文档字段的数据类型为数字时,该规则将失败-

Below is a sample rule that fails when the document field used in the condition has a data type of number-

match message_box/{user_id}/inbox {
      allow read: if get('..../users/$(request.auth.uid)).data['user_id'] == user_id
}

此规则始终失败,因为在数据中,字段值是数字,但是在路径变量{user_id}中,它被捕获为字符串.

This rule always fails because in data the field value is number, but in path variable {user_id} it is captured as string.

以下两项测试证实这是一个实际问题-

Following two tests validates that it is an actual issue-

  1. 在规则中,通过在比较右侧使用硬编码的整数值(例如-data ['user_id'] == 456),然后读取成功.或-
  2. 在数据中,将user_id字段的数据类型设置为字符串.在这种情况下 此帖子顶部指定的匹配规则成功.
  1. In rules, by using a hardcoded integer value on right side of the comparison (e.g.- data['user_id'] == 456), and then the read succeeds. Or-
  2. In data, set the data type of user_id field to string. In that case the match rule specified at the top of this post succeeds.

有什么简单的方法可以使其顺利运行?在规则文档中看不到将int转换为字符串的方法.

Any easy way to run it smooth? Don't see a way to cast int to string in rules documentation.

推荐答案

万一将来有人遇到这种情况,请查看以下有关在Firestore规则中转换数据类型的文档:

In case anyone runs into this in the future, check out these docs on converting data types in the Firestore rules:

字符串->整数

整数->字符串

示例:

int("2") == 2
int(2.0) == 2

string(true) == "true"
string(1) == "1"
string(2.0) == "2.0"
string(null) == "null"

这篇关于Firestore规则:当数据为数字时,条件检查失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆