Pra*_*ant 1 android google-cloud-firestore
I am new to android development and firestore. What I am trying to do is compare whether a string exists in the database or not. However, the query always returns true even if I am trying to compare with string that is not present in the database. Here is my code:
public void loadData(View v){
db.collection("Machines")
.whereEqualTo("machineId","AYSLE004")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
//MachineId exists
Toast.makeText(MainActivity.this, "Machine Id exists", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Machine Id does not exist
Toast.makeText(MainActivity.this, "Machine Id does not exist", Toast.LENGTH_LONG).show();
Log.d(TAG,e.toString());
}
});
}
Run Code Online (Sandbox Code Playgroud)
The string "AYSLE004" does not exist in the database. However the toast message shows "Machine Id exists". Please assist me where I am going wrong. Thanks in advance.

尝试添加以下内容:
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
//MachineId exists
if(queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
Toast.makeText(MainActivity.this, "Machine Id exists", Toast.LENGTH_LONG).show();
} else {
//it doesn't exist
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样做的原因(我认为)是因为onSuccess调用Firebase是因为对Firebase的请求已完成并实现successfully,只是没有找到您的请求。
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |