Firestore的whereEqualTo始终返回true。

huangapple go评论83阅读模式
英文:

firestore whereEqualTo always get returns true

问题

我想在我的Firestore数据库中设置一个监听器。

我想监听一个名为 isConfirmClicked 的值,如果它等于1,我想知道。

我使用以下代码:

db.collection("Users").document(NotMyID).collection("MyChats").whereEqualTo("isConfirmClicked", 1).addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot value,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            return;
        }

        在这里执行一些操作

    }
});

由于某种原因,即使确实 isConfirmClicked 等于0,它总是进入到 在这里执行一些操作 部分。

这是否有任何原因?

英文:

I want to set a listener in my firestore database.

I want to listen to a value called isConfirmClicked and in case it equals 1 I want to know about it.

I use the following:

db.collection( &quot;Users&quot; ).document( NotMyID ).collection( &quot;MyChats&quot; ).whereEqualTo( &quot;isConfirmClicked&quot; , 1 ).addSnapshotListener( new EventListener&lt;QuerySnapshot&gt;() {
    @Override
    public void onEvent(@Nullable QuerySnapshot value,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            return;
        }

		DO SOMETHING HERE
		
    }
} );

For some reason, it always enters to the DO SOMETHING HERE even when for sure isConfirmClicked equals to 0.

Firestore的whereEqualTo始终返回true。

Is there any reason for this?

答案1

得分: 1

你提供的监听器会在查询结果可用时被调用,即使没有匹配的文档。如果你想要知道查询是否有匹配的文档,你需要使用提供的 QuerySnapshot 并使用它的 isEmpty() 方法进行检查。

@Override
public void onEvent(@Nullable QuerySnapshot querySnapshot,
                    @Nullable FirebaseFirestoreException e) {
    if (e != null) {
        return;
    }

    if (!querySnapshot.isEmpty()) {
        // 这里至少有一个匹配查询的文档
    }        
}

或者,你可以检查 querySnapshot.size() 是否大于 0。

英文:

The listener you provide will be invoked whenever the results are known from the query, even if there are no matching documents. If you want to know if there are matching documents for your query, you will need to check the provided QuerySnapshot using its isEmpty() method.

@Override
public void onEvent(@Nullable QuerySnapshot querySnapshot,
                    @Nullable FirebaseFirestoreException e) {
    if (e != null) {
        return;
    }

    if (!querySnapshot.isEmpty()) {
        // here, there is at least one document that matches the query
    }        
}

Or, you can check if querySnapshot.size() > 0.

huangapple
  • 本文由 发表于 2020年8月16日 04:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63430411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定