【问题标题】:Why is Firestore still producing error after composite index creation?为什么创建复合索引后 Firestore 仍然产生错误?
【发布时间】:2020-01-29 21:37:29
【问题描述】:

我有一个名为“用户”的 Firestore 集合。 这是我有兴趣执行的三个 DB 读取:

1:

const onNewestChange = (dispatch) => {
    firebase
        .firestore()
        .collection('users')
        .where('role', '==', 'a')
        .where('active', '==', true)
        .orderBy('createdAt')
        .limit(10)
        .onSnapshot((querySnapshot) => {
            const newestProfiles = querySnapshot.forEach((queryDocSnapshot) => {
                const profile = queryDocSnapshot.data();
                newestProfiles.push(profile);
            });
            dispatch({ type: types.LOAD_NEWEST, payload: newestProfiles });
        });
};

2:

const onMostPopularChange = (dispatch) => {
    firebase
        .firestore()
        .collection('users')
        .where('role', '==', 'a')
        .where('active', '==', true)
        .orderBy('lifetimeInbox')
        .limit(10)
        .onSnapshot((querySnapshot) => {
            const popularProfiles = querySnapshot.forEach((queryDocSnapshot) => {
                const profile = queryDocSnapshot.data();
                popularProfiles.push(profile);
            });
            dispatch({ type: types.LOAD_POPULAR, payload: popularProfiles });
        });
};

3:

const onMostActiveChange = (dispatch) => {
    firebase
        .firestore()
        .collection('users')
        .where('role', '==', 'a')
        .where('active', '==', true)
        .orderBy('lifetimeRead')
        .limit(10)
        .onSnapshot((querySnapshot) => {
            const activeProfiles = querySnapshot.forEach((queryDocSnapshot) => {
                const profile = queryDocSnapshot.data();
                activeProfiles.push(profile);
            });
            dispatch({ type: types.LOAD_ACTIVE, payload: activeProfiles });
        });
};

示例“用户”文档具有以下字段:

  • 用户名
  • 法定姓名
  • 角色
  • 活跃
  • 创建于
  • lifetimeInbox
  • lifetimeRead

我创建了以下复合索引:

也试过了:

我的问题是我试图解决的初始错误即使在创建复合索引后仍然存在

错误:Firestore:操作被拒绝,因为系统未处于操作执行所需的状态。 (firestore/failed-precondition)

可以做些什么来解决这个问题?

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    在将我的工作限制在onNewestChange() 并使用不同的复合索引之后,我意识到where()orderBy() 参数中的所有内容以及被查询的集合都需要包含在内。所以在我的情况下......

    1. 需要一个包含字段的复合索引

      • 角色(升序)
      • 活动(升序)
      • createdAt (asc)
    2. 需要带字段的复合索引

      • 角色(升序)
      • 活动(升序)
      • lifetimeInbox (asc)
    3. 需要带字段的复合索引

      • 角色(升序)
      • 活动(升序)
      • lifetimeRead (asc)

    现在错误消失了。

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2023-02-15
      • 2020-01-22
      相关资源
      最近更新 更多