【问题标题】:Why is Firestore array-contains not working?为什么 Firestore 阵列包含不起作用?
【发布时间】:2021-03-04 22:39:30
【问题描述】:

我有一个非常基本的查询,我在我的应用程序中使用 react native 和 firestore webconsole 运行,但它们都没有返回结果。我的数据不正确还是我的查询?我有一个 ids 数组作为属性,并希望在数组中查找所有值为 114 的文档。

我的 react native 应用在 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

【问题讨论】:

  • 请编辑您的问题以显示完整的查询,以及您如何读取/处理数据,而不仅仅是您认为导致问题的 where 子句。我强烈推荐阅读how to create a minimal, complete, verifiable example,以获得更多建议,最大限度地提高他人提供帮助的机会。
  • 我已经复制了您的问题,我可以看到“array-contains”成功运行。但是,在您的第一个屏幕截图中,我看不到您在集合“lift_tracker_groups”中有任何文档,并且您正在尝试使用“array-contains”进行过滤。在第二个屏幕截图中,我可以看到您有文档,但我看不到您正在尝试过滤而不是获取文档。请提供更多信息,说明究竟是什么不起作用。
  • 所以我进一步挖掘并发现我的问题与数组内部的反应原生创建数字有关。这是我制作的视频链接,用于展示问题drive.google.com/file/d/1nMS4P32BX9dPirhLJIm2qDzx0_XBxdPb/…。我将使用我的 react 组件和 firestore 版本号更新此页面。
  • 谜团更深了,如果你用 react native 代码创建了一个数字数组,你可以在 react native 代码中查询数据,但不能在 web 控制台中查询。如果您使用 Web 控制台创建数字数组,则无法使用 react 本机代码查询它们。如果您使用 web 控制台更新使用 react native 代码创建的数据,您也不能再使用 react native 查询它。

标签: javascript react-native google-cloud-firestore


【解决方案1】:

我已经重现了您的场景,并在查询特定文档时做了一些测试:

"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/console 中的隐式转换会产生意想不到的结果。

我发现这个 GitHub issue 已打开,请评论他们关于您的问题,因为它似乎是一个 React Native 问题。

或者,您可以向 Firebase 支持团队创建 bug Report

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 2010-10-23
    • 2013-05-04
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多