gpt4 book ai didi

java - 为什么我不能在查询 Firestore 中使用 whereEqualTo 条件

转载 作者:行者123 更新时间:2023-11-29 22:52:06 24 4
gpt4 key购买 nike

我想做的是先检查用户输入的推荐代码是否存在。但问题是,即使我输入的文本不包含在数据库中,它也总是在 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]: /image/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 的用户。另请注意,您类(class)中的字段与数据库中的字段对应。为此,请参阅我在以下帖子中的回答:

因此,如果您想将数据作为 Users 类的对象获取,请使用该答案中的建议。

关于java - 为什么我不能在查询 Firestore 中使用 whereEqualTo 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com