我们可以在 firebase 中的多个属性上使用不等式过滤器吗

问题描述 投票:0回答:1

这个答案提到:

Firestore 最近添加了不平等和范围的能力 单个查询中多个字段的条件

但是我在尝试这样做时遇到了错误。 这是错误: Error message shown in firebase emulator logs 这是我的代码:

let firestoreQuery = usersSubsRef.where("subscriberUid", "in", userIds);

  // Filter by status
  if (selectedStatuses.length !== 0 && !selectedStatuses.includes(Status.All)) {
    firestoreQuery = firestoreQuery.where("status", "in", selectedStatuses);
  }

  // Filter by lifetimeSpend
  const fromAmount = parseFloat(lifetimeSpend[RangeKeys.From]);
  const toAmount = parseFloat(lifetimeSpend[RangeKeys.To]);

  if (!isNaN(fromAmount)) {
    firestoreQuery = firestoreQuery
      .where("lifeTimeSpend", ">=", fromAmount * 100)
      .orderBy("lifeTimeSpend", "desc");
  }
  if (!isNaN(toAmount)) {
    firestoreQuery = firestoreQuery
      .where("lifeTimeSpend", "<=", toAmount * 100)
      .orderBy("lifeTimeSpend", "desc");
  }

  // Filter by joinDate
  if (joinDate !== "Anytime") {
    const today = new Date();
    const thisWeekStart = startOfWeek(today, { weekStartsOn: 1 });
    const thisWeekEnd = endOfWeek(today, { weekStartsOn: 1 });
    const lastWeekStart = startOfWeek(subWeeks(today, 1), { weekStartsOn: 1 });
    const lastWeekEnd = endOfWeek(subWeeks(today, 1), { weekStartsOn: 1 });
    const thisMonthStart = startOfMonth(today);
    const thisMonthEnd = endOfMonth(today);
    const lastMonthStart = startOfMonth(subMonths(today, 1));
    const lastMonthEnd = endOfMonth(subMonths(today, 1));
    switch (joinDate) {
      case "This Week":
        firestoreQuery = firestoreQuery
          .where("startDate", ">=", thisWeekStart)
          .where("startDate", "<=", thisWeekEnd);
        break;
      case "Last Week":
        firestoreQuery = firestoreQuery
          .where("startDate", ">=", lastWeekStart)
          .where("startDate", "<=", lastWeekEnd);
        break;
      case "This Month":
        firestoreQuery = firestoreQuery
          .where("startDate", ">=", thisMonthStart)
          .where("startDate", "<=", thisMonthEnd);
        break;
      case "Last Month":
        firestoreQuery = firestoreQuery
          .where("startDate", ">=", lastMonthStart)
          .where("startDate", "<=", lastMonthEnd);
        break;
      case "Custom Range": {
        const from = parse(
          customRange[RangeKeys.From],
          "MMM d/yyyy",
          new Date()
        );
        const to = parse(customRange[RangeKeys.To], "MMM d/yyyy", new Date());
        firestoreQuery = firestoreQuery
          .where("startDate", ">=", from)
          .where("startDate", "<=", to);
        break;
      }
    }
  }

  // Filter by subscriptionDuration
  if (subscriptionDuration.number) {
    // make sure subscription is active first
    firestoreQuery = firestoreQuery.where("status", "==", "active");
    const { comparison, number, unit } = subscriptionDuration;
    const numValue = parseInt(number, 10);
    const today = new Date();
    let comparisonDate: Date;

    switch (unit) {
      case DurationUnit.Weeks:
        comparisonDate = subWeeks(today, numValue);
        break;
      case DurationUnit.Months:
        comparisonDate = subMonths(today, numValue);
        break;
      case DurationUnit.Years:
        comparisonDate = subYears(today, numValue);
        break;
      default:
        throw new Error("Invalid duration unit");
    }

    if (comparison === Comparison.AtLeast) {
      firestoreQuery = firestoreQuery.where(
        "startDate",
        "<=",
        startOfDay(comparisonDate)
      );
    } else if (comparison === Comparison.Exactly) {
      firestoreQuery = firestoreQuery
        .where("startDate", ">=", startOfDay(comparisonDate))
        .where("startDate", "<", endOfDay(comparisonDate));
    }
  }

firebase-admin 版本是 12.3.1,firebase-functions 版本是 5.0.1。

node.js firebase google-cloud-firestore google-cloud-functions
1个回答
0
投票

将 firebase-tools 更新到最新的 13.15.1 可以解决该问题。

© www.soinside.com 2019 - 2024. All rights reserved.