java - Firestore whereEqualto()在Android应用程序中查询的不正确行为
问题描述
我的数据库由一个名为的集合组成,该集合具有songs
以下字段:artist
、email
和。目前所有文档中的字段都是空字符串。这是其中一个条目的图片。link
name
email
这是我在我的 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");
}
}
});
}
User found
即使所有电子邮件条目都是空字符串,TextView 仍在打印。预期的行为是User not found
。
解决方案
如果你只检查一个任务是否成功,并不意味着你得到了结果。这意味着您的查询已完成,没有错误。如果您需要知道查询是否返回所需的结果,那么您应该在代码中检查:
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。
推荐阅读
- r - R代码:向现有数据添加一列(平均分数)。帧不返回正确的数字
- neural-network - Pytorch 中的预训练模型
- python - 如何在深度优先遍历中分离路线?
- node.js - “请求正文”未出现在 Swagger UI 上
- r - 避免重新排序热图中的行和列 (r)
- javascript - Flexbox 不包含整个图像 (CSS)
- node.js - Angular Universal 使用全局浏览器对象,如 window 和 localstorage
- android - Appinventor - Webview does not open app link
- powershell - Disable Hash Result?
- lua - 未找到模块“cudnn”:未找到 cudnn 的 LuaRocks 模块