【问题标题】:Why is Firestore rejecting the operation?为什么 Firestore 拒绝该操作?
【发布时间】:2020-02-09 00:37:35
【问题描述】:

我在 react-native 项目中有一个 firestore 操作:

export const getMessages = (convoId) => {
    return (dispatch) => {
        firebase
            .firestore()
            .collection('messages')
            .where('userConvos', 'array-contains', convoId)
            .orderBy('time')
            .onSnapshot((querySnapshot) => {
                const messages = [];
                querySnapshot.forEach((doc) => {
                    messages.push(doc.data());
                });
                dispatch({ type: types.LOAD_MSGS, payload: messages });
            });
    };
};

我在 firestore 中尝试过的两个复合索引:

而我的数据库结构是这样的:

  • 消息(集合)
    • 内容(字段)
    • 时间(场)
    • userConvos(字段)

问题是当我拨打这个 firestore 电话时,我得到了

错误:Firestore:操作被拒绝,因为系统未处于操作执行所需的状态

但如果错误被消除,数据将按预期排序。这是怎么回事?如何消除此错误?

【问题讨论】:

  • 你试过用谷歌搜索那个错误信息吗?其他地方已经讨论过了。
  • 是的,我发现的只是因为查询,需要在 firestore 中建立索引

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


【解决方案1】:

您缺少索引。要创建它,请执行以下操作。

在终端输入查看安卓设备日志:

adb logcat 

如果您重现错误,那么日志中将出现类似以下内容:

05-16 04:23:20.887 3746 3895 我 ReactNativeJS: nativeErrorMessage: 'FAILED_PRECONDITION:查询需要索引。你可以创建它 这里: https://console.firebase.google.com/v1/r/project/XXXXXXXXX/firestore/indexes?create_composite=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',

打开浏览器并粘贴链接。

按下Create Index 按钮。

【讨论】:

    猜你喜欢
    • 2017-09-14
    • 2020-09-12
    • 2019-10-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2020-07-04
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多