我想做的是先检查用户插入的推荐代码是否存在。但是问题是,即使我输入了数据库中未包含的文本,它也始终在addOnSuccessListener上进行。
//不要介意下一段。我只需要添加一些描述来保存编辑//
我想做的是先检查用户插入的推荐代码是否存在。但是问题是,即使我输入了数据库中未包含的文本,它也始终在addOnSuccessListener上进行。
我想做的是先检查用户插入的推荐代码是否存在。但是问题是,即使我输入了数据库中未包含的文本,它也始终在addOnSuccessListener上进行。
///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// //////////
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference collectionReference = db.collection("Buyers");
Query usersDataQuery = collectionReference.whereEqualTo("referrals.userReferralCOde", referalCode);
usersDataQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
Users users = new Users();
String referedByID = users.getBuyerID();
loadingBar = KProgressHUD.create(FirstTimeUserActivity.this)
.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
.setLabel("Please wait")
.setDetailsLabel("Creating your Account...")
.show();
//withReferrals(cfn, cln, referedByID, email, status, id);
StyleableToast.makeText(FirstTimeUserActivity.this, "Pumasok.", Toast.LENGTH_LONG, R.style.successtoast).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
StyleableToast.makeText(FirstTimeUserActivity.this, "Please enter a Valid Referral Code.", Toast.LENGTH_LONG, R.style.errortoast).show();
}
public class Users {
private String BuyerID, Email, FirstName, LastName, ImageUrl, AccountType, CPoints, WalletAccount;
private double Latitude, Longitude;
private @ServerTimestamp Date timestamp;
private ReferralModel Referrals;
public Users(){
}
public Users(String buyerID, String email, String firstName, String lastName, String imageUrl, String accountType, String CPoints, String walletAccount, double latitude, double longitude, Date timestamp, ReferralModel referrals) {
BuyerID = buyerID;
Email = email;
FirstName = firstName;
LastName = lastName;
ImageUrl = imageUrl;
AccountType = accountType;
this.CPoints = CPoints;
WalletAccount = walletAccount;
Latitude = latitude;
Longitude = longitude;
this.timestamp = timestamp;
Referrals = referrals;
}
public String getBuyerID() {
return BuyerID;
}
public void setBuyerID(String buyerID) {
BuyerID = buyerID;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getImageUrl() {
return ImageUrl;
}
public void setImageUrl(String imageUrl) {
ImageUrl = imageUrl;
}
public String getAccountType() {
return AccountType;
}
public void setAccountType(String accountType) {
AccountType = accountType;
}
public String getCPoints() {
return CPoints;
}
public void setCPoints(String CPoints) {
this.CPoints = CPoints;
}
public String getWalletAccount() {
return WalletAccount;
}
public void setWalletAccount(String walletAccount) {
WalletAccount = walletAccount;
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double latitude) {
Latitude = latitude;
}
public double getLongitude() {
return Longitude;
}
public void setLongitude(double longitude) {
Longitude = longitude;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public ReferralModel getReferrals() {
return Referrals;
}
public void setReferrals(ReferralModel referrals) {
Referrals = referrals;
}
});[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/Jr9Vm.png
最佳答案
除了您的代码referrals.userReferralCOde
中应该有错字而应该是referrals.userReferralCode
的事实之外,您还没有从数据库中获取任何Users
对象,因为您正在创建一个新的对象。您可以简单地使用addOnCompleteListener
,您会看到QuerySnapshot
对象包含来自数据库的数据。要查看结果,您应该像下面的代码行那样进行迭代:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference buyersReference = rootRef.collection("Buyers");
Query usersDataQuery = buyersReference.whereEqualTo("referrals.userReferralCode", "123");
usersDataQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String firstName = document.getString("firstName");
Log.d(TAG, firstName);
}
}
}
});
logcat中的结果将是所有
userReferralCode
等于123
的用户。另请注意,您班级中的字段与数据库中的字段不对应。为此,请从以下帖子中查看我的答案:Images not loading from URL by Picasso from Firebase Database
因此,如果您想获取数据作为
Users
类的对象,请使用该答案中的建议。