首页 > 解决方案 > 为什么 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

标签: javascriptreact-nativegoogle-cloud-firestore

解决方案


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

"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 支持团队创建错误报告。


推荐阅读