【问题标题】:Incorrect behaviour by Firestore whereEqualto() query in android appFirestore whereEqualto()在Android应用程序中查询的不正确行为
【发布时间】:2021-11-14 17:47:02
【问题描述】:

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

这是我在我的 android 应用程序中运行的查询:

    @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");
        }
    }
});


    }

TextView 正在打印User found,即使所有电子邮件条目都是空字符串。预期的行为是User not found

【问题讨论】:

    标签: java android firebase google-cloud-platform google-cloud-firestore


    【解决方案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 TextView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多