[英]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.