【问题标题】:Firestore Query whereEqualTo along with whereGreaterThan not working [duplicate]Firestore查询whereEqualTo以及whereGreaterThan不起作用[重复]
【发布时间】:2020-09-18 13:52:50
【问题描述】:

当我用whereGreaterThan 查询whereEqualTo 时:

productRef
            .whereGreaterThan("discount",0)
            .whereEqualTo("available", true)
            .whereEqualTo("shopId", Pref.getSelectedShopId(activity))
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
                                    @Nullable FirebaseFirestoreException e) {
                    for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            Product product = doc.getDocument().toObject(Product.class);
                            productList.add(product);
                            myAdapter.setData(productList);
                            myAdapter.notifyDataSetChanged();
                        }
                    }
                }
            });

我收到此错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()”

【问题讨论】:

  • 你有这个查询的复合索引吗?
  • 没有。我不使用

标签: android google-cloud-firestore


【解决方案1】:

如果没有复合索引,您不能在查询中放置具有不同字段的多个条件。

您可以在官方文档中找到更多信息:
https://firebase.google.com/docs/firestore/query-data/index-overview
https://firebase.google.com/docs/firestore/query-data/indexing

顺便说一句,firebase 会创建一个链接来创建这些索引,通常,它可以在控制台中找到(android 案例中的 logcat)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多