有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓应用程序中Firestore whereEqualto()查询的java错误行为

我的数据库由一个名为songs的集合组成,该集合有以下字段:artistemaillinkname。目前,所有文档中的email字段都是空字符串。这是一张照片of one of the entries.

以下是我在安卓应用程序中运行的查询:

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        firebaseAuth = FirebaseAuth.getInstance();

        FirebaseFirestore db = FirebaseFirestore.getInstance();

        TextView userinfo = (TextView)findViewById(R.id.link);
        FirebaseUser userauth = firebaseAuth.getCurrentUser();

db.collection("songs").whereEqualTo("email",userauth.getEmail()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>(){
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            // Document found in the offline cache
            userinfo.setText("User found");
        } else {
           userinfo.setText("User not found");
        }
    }
});


    }

文本视图正在打印User found,尽管所有电子邮件条目都是空字符串。预期的行为是User not found


共 (1) 个答案

  1. # 1 楼答案

    如果你只检查一项任务是否成功,并不意味着你得到了结果。这意味着您的查询已完成,没有错误。如果需要知道查询是否返回所需的结果,则应在代码中检查:

    if (task.isSuccessful()) {
        QuerySnapshot snapshot = task.getResult();
        if (snapshot.isEmpty()) {
            userinfo.setText("User not found");
        } else {
            userinfo.setText("User found");
        }
    } else {
        Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
    }
    

    “找不到用户”消息将设置为userinfo文本视图