Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何知道firestore查询快照中是否存在数据?_Java_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Java 如何知道firestore查询快照中是否存在数据?

Java 如何知道firestore查询快照中是否存在数据?,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,这是我的代码,我想在其中获取数据,并知道数据是否存在。 问题是,若数据存在,它将运行{if(task.issusccessful()},但若数据不存在,它将不执行任何操作 我怎么知道数据不存在呢?我添加了其他{else}语句,但它不起作用 CollectionReference reference = firestore.collection("Carts").document(FirebaseAuth.getInstance().getCurrentUser().getUid())

这是我的代码,我想在其中获取数据,并知道数据是否存在。 问题是,若数据存在,它将运行
{if(task.issusccessful()}
,但若数据不存在,它将不执行任何操作

我怎么知道数据不存在呢?我添加了其他
{else}
语句,但它不起作用

CollectionReference reference = firestore.collection("Carts").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .collection("Carts");
    reference.whereEqualTo("ordered",false).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    if(document.exists()){
                        Toast.makeText(ListProducts.this, "Exists", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        // This part is not running even if there is no data
                        Toast.makeText(ListProducts.this, "NOPE", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    });
CollectionReference=firestore.collection(“Carts”).document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.托收(“推车”);
reference.whereEqualTo(“ordered”,false).get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(QueryDocumentSnapshot文档:task.getResult()){
if(document.exists()){
Toast.makeText(ListProducts.this,“Exists”,Toast.LENGTH_SHORT.show();
}
否则{
//即使没有数据,此部件也不会运行
Toast.makeText(ListProducts.this,“NOPE”,Toast.LENGTH_SHORT.show();
}
}
}
}
});

您需要直接在
QueryDocumentSnapshot
对象上使用
exists()
方法,如下所示:

CollectionReference reference = firestore.collection("Carts").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .collection("Carts");
    reference.whereEqualTo("ordered",false).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    if (document.exists()) {
                         Toast.makeText(ListProducts.this, document.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            } else{
                //This Toast will be displayed only when you'll have an error while getting documents.
                Toast.makeText(ListProducts.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });
CollectionReference=firestore.collection(“Carts”).document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.托收(“推车”);
reference.whereEqualTo(“ordered”,false).get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(QueryDocumentSnapshot文档:task.getResult()){
if(document.exists()){
Toast.makeText(ListProducts.this,document.toString(),Toast.LENGTH_SHORT).show();
}
}
}否则{
//只有在获取文档时出错时,才会显示此Toast。
Toast.makeText(ListProducts.this,task.getException().toString(),Toast.LENGTH_SHORT).show();
}
}
});
task.issusccessful()
用于处理侦听器中的成功或失败,而方法在对扩展类的类的对象调用时返回:

如果此快照中存在文档,则为true


使用isEmpty()或size()方法成功完成任务时,检查文档是否存在


我知道我有点晚了,不知道你是否找到了答案。但我刚刚知道怎么做:

reference.whereEqualTo("ordered",false).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

    if (queryDocumentSnapshots.isEmpty()) {
        Snackbar.make(mainContactsLayout, "No matches found", Snackbar.LENGTH_SHORT).show();
    } else {

        for (DocumentSnapshot documentSnapshot : queryDocumentSnapshots.getDocuments()) {
            Snackbar.make(mainContactsLayout, documentSnapshot.getId(), Snackbar.LENGTH_SHORT).show();
        }
    }

}
});
reference.whereEqualTo(“ordered”,false).get().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(QuerySnapshot QueryDocumentSnapshot){
if(queryDocumentSnapshots.isEmpty()){
Snackbar.make(主联系人slayout,“未找到匹配项”,Snackbar.LENGTH_SHORT.show();
}否则{
对于(DocumentSnapshot DocumentSnapshot:queryDocumentSnapshots.getDocuments()){
make(mainContactsLayout,documentSnapshot.getId(),Snackbar.LENGTH_SHORT).show();
}
}
}
});

您需要验证文档列表是否为空

FirebaseFirestore.getInstance().collection(collectionName).whereEqualTo(cle
            , valeur)
            .get()
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    QuerySnapshot querySnapshot = task.getResult();
                        if (querySnapshot.isEmpty()) { // no documents found
                            // Type your code yere
                        } else {
                            for (QueryDocumentSnapshot document : querySnapshot){

                            }
                        }
                } else {
                    Log.d(TAG, methodName + task.getException());
                }
            });
如果我执行

for (int i = 2; i < 1; i ++) { }
for(inti=2;i<1;i++){

这没关系,但我们永远不会进入这个循环。上面的代码也一样。

尝试过这个,只在数据存在时有效,如果数据不存在,它将不返回任何内容,即使我添加了else语句或(!document.exists())它不起作用!如果它只在数据存在时起作用,则表示您没有查找不存在的数据。因此,不调用If语句的第二部分是正常的。但是,这是唯一一种方法,可以在查询快照中显示数据是否存在。请参阅我在我的更新的答案。我更新了问题(代码部分)我评论了即使没有数据也不运行的部分!逻辑上,该部分必须在没有数据时运行,但它不运行!
FirebaseFirestore.getInstance().collection(collectionName).whereEqualTo(cle
            , valeur)
            .get()
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    QuerySnapshot querySnapshot = task.getResult();
                        if (querySnapshot.isEmpty()) { // no documents found
                            // Type your code yere
                        } else {
                            for (QueryDocumentSnapshot document : querySnapshot){

                            }
                        }
                } else {
                    Log.d(TAG, methodName + task.getException());
                }
            });
for (int i = 2; i < 1; i ++) { }