[英]How to search from the cloud-firestore database?
我希望用戶鍵入他們的電子郵件和密碼。 身份驗證后,我想根據他們的電子郵件檢查他們是否為管理員,然后打開其他活動。 我應該如何執行搜索查詢? 可能嗎
以下是對我有用的答案。 有關一般情況,請參見亞歷克斯回答。
mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
rootRef.collection("Users").whereEqualTo("Email","ashish@gmail.com").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
if (document.getString("Admin").equals("Yes")) {
Toast.makeText(Login.this, "Logged In!", Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(Login.this, MainActivity.class));
} else {
Toast.makeText(Login.this, "Logged In!", Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(Login.this, nonadmin.class));
}
}
} else {
mProgressBar.setVisibility(View.GONE);
Toast.makeText(Login.this, "Sign In Problem", Toast.LENGTH_LONG).show();
}
}
});
}
}
});
要解決此問題,請使用以下代碼:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collection("Users").whereEqualTo("Email", "ashish@startup.com").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
if (document.getString("Admin").equals("Yes")) {
Log.d(TAG, "User is Admin!");
}
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
輸出將是: User is Admin!
。
也不要忘記像這樣設置您的安全規則:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.