在 firestore 中查询 doc 的子项时,Flutter 操作被拒绝

标签 flutter google-cloud-firestore

我正在开发电子商务应用程序和优惠券系统,同时创建查询我每次都会收到此错误。

FirebaseException([cloud_firestore/failed-precondition] 操作被拒绝,因为系统未处于操作执行所需的状态。如果执行查询,请确保已通过 Firebase 控制台对其进行索引。)

我试图搜索错误,但找不到确切的解决方案,因为一些答案表明我必须创建一个集合索引,我这样做了,但仍然遇到相同的错误。

enter image description here

我的查询是这样的:-

Future<void> fetchCouponCode(
    String couponCode,
    String userId,
    String priductCategory,
    String productIds,
) async {
   if (couponCode == null) {
      return;
   }
   if (userId == null) {
      return;
   }
   print(couponCode);
   var today = DateTime.now();
   await FirebaseFirestore.instance
      .collection('coupons')
      .where('code', isEqualTo: couponCode)
      .where('product_categories', isEqualTo: priductCategory)
      .where('customer_email', isEqualTo: 'all')
      .where('product_ids', arrayContainsAny: [productIds])
      // .where('constraints.BagConstraints.min_item', isGreaterThan: 1)
      .where('is_enabled', isEqualTo: true)
      .where('valid_from',
        isGreaterThanOrEqualTo: today)
      .get()
      .then((QuerySnapshot querySnapshot) {
          print('=========object=======');
          querySnapshot.docs.forEach((element) {
              print(element.get('discount'));
          });
          
       });
  }

我的优惠券结构是这样的

   var uuid = Uuid();
  {
    'coupon_id': uuid.v4(),
    'code': 'V710OFF',
    'description': 'Summer Sale',
    'discount': 40,
    'minimum_dicount': 30,
    'maximum_discount': 150,
    'discount_type': 'percent',
    'product_ids': ['3'],
    'date_created': DateTime.now(),
    'exclude_product_ids': 2,
    'product_categories': 't-shirts',
    'exclude_product_categories': '',
    'customer_email': 'all',
    'constraints': {
      'commonConstraints': {
        'uniqueId': uuid.v4(),
        'device_type': 'ios',
        'valid_from': DateTime.now(),
        'valid_until': DateTime.now().add(Duration(days: 10)),
        'usage_limit': 1,
        'usage_limit_per_user': 1,
        'total_usage': 10
      },
      'BagConstraints': {
        'uniqueId': uuid.v4(),
        'min_value': 120,
        'max_value': 3000,
        'min_item': 1,
        'max_item': 4,
      }
    },
    'is_enabled': true
  }

enter image description here

我需要检查优惠券的日期范围是否无效。所以我应该在 querySnapshot.docs.forEach 中做,因为这可以完成,但如果我对另一个集合有类似的问题怎么办。怎么查询三级文档或者现在不能实现??我还浏览了 firestore 文档和 youtube channel ,但找不到解决方案

这是我查询优惠券的方式

fetchCouponCode(
    'V710OFF', currentFirebaseUser.uid, 't-shirts', '3');

最佳答案

您创建的索引不适用于您尝试执行的查询。尝试创建这个索引:

enter image description here

<表类="s-表"> <头> 集合ID 字段索引 类型 <正文> 优惠券 product_ids 数组 代码 升序 客户邮箱 升序 已启用 升序 产​​品类别 升序 有效时间 升序

关于在 firestore 中查询 doc 的子项时,Flutter 操作被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68405758/

相关文章:

dart - 如何在同一设备上为不同项目使用两个版本的 Flutter?

Flutter SizeTransition 和 PageRouteBuilder 不工作

android - 使用 flutter 应用更改设备亮度

flutter - 为GridView中的其他所有项目增加值(value)

javascript - 将格式化的 int 保存到 Javascript 的 firestore

linux - flutter create命令尝试访问根目录并失败

ios - 过滤唯一值以在消息收件箱中显示唯一用户名

javascript - 如何使用 React/Firestore 获取单个文档?

firebase - Flutter中的定时支持聊天

java - 按数据范围和另一个过滤器搜索