javascript - 为什么 Firestore 阵列包含不起作用?
问题描述
我有一个非常基本的查询,我在我的应用程序中使用 react native 和 firestore webconsole 运行,它们都没有返回结果。我的数据不正确还是我的查询?我有一个 id 数组作为属性,并希望在数组中找到所有值为 114 的文档。
我的反应原生应用程序在 ios 14.4 上运行并运行“@react-native-firebase/storage”:“^11.0.0”。
import React from 'react';
import {View} from 'react-native';
import {Button} from 'material-bread';
import firestore from '@react-native-firebase/firestore';
import {uuidv4} from '../../utils/uuid';
const TestPage = () => {
return (
<View style={{paddingTop: 140}}>
<Button
text={'Add test data record'}
type="flat"
textColor={'#FFFFFF'}
color={`#000000`}
fullWidth
onPress={() => {
firestore()
.collection('Testing')
.doc(uuidv4())
.set({
exercise_ids: [
112,
114,
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
],
})
.then(() => {})
.catch((err) => {
console.log(err);
});
}}
/>
</View>
);
};
export default TestPage;
询问
.where("exercise_ids", "array-contains", 114)
数据
{exercise_ids: [114,25]}
问题视频https://drive.google.com/file/d/1nMS4P32BX9dPirhLJIm2qDzx0_XBxdPb/view?usp=drive_web
解决方案
我已经重现了您的场景,并且在查询特定文档时做了一些测试:
"test_array": {
"arrayValue": {
"values": [
{
"integerValue": "1"
},
{
"integerValue": "2"
},
{
"doubleValue": 0.001
},
{
"integerValue": "3"
}
]
}
}
所以根据显示的文档格式,这些都是数字,但实际上数据类型是ints / doubles。
所以你的React Native应用程序正在上传 double 类型的数字。这就是为什么查询(被解释为 int)与 double 值不匹配的原因。
换句话说,查询时的值 2 (int) != 2 (double)。
请注意,此问题特定于 web,因为 JS 在两种数据类型之间没有区别。他们都是数字。所以 web sdk / 控制台中的隐式转换会产生意想不到的结果。
我发现这个GitHub 问题已打开,请就您的问题发表评论,因为它似乎是 React Native 问题。
或者,您可以向 Firebase 支持团队创建错误报告。
推荐阅读
- kotlin - Kotlin 扩展方法覆盖
- spring-boot - JSON解析错误:无法在spring boot中反序列化来自String \"2020-09-07T00:00:00.0+05:30的`java.time.LocalDateTime`类型的值
- r - 使用 dplyr 将列名作为函数输入包含在内
- python - Django - 显示分配给帖子的所有图像
- excel - 如何在excel中设置固定的列宽?
- javascript - 停止 table2excel 将 50:10 之类的比率值视为导出 excel 中的时间
- javascript - 使用 Javascript 将当前日期转换为 IST 格式
- javascript - 打印分页符 Javascript
- php - 要显示的 Laravel API 验证错误
- android - 如何知道为什么接收者没有调用类并且 onReceive 没有执行