Mr. Baks
Mr. Baks

Reputation: 317

Why I can't use the whereEqualTo condition in query Firestore

What I want to do is check first if The referral code inserted by the user exist. But the problem is, it is always going on the addOnSuccessListener even though I had entered text which are not included in the database.

//Dont mind the next paragraph. I just need to add some more descriptions to save the edit//

What I want to do is check first if The referral code inserted by the user exist. But the problem is, it is always going on the addOnSuccessListener even though I had entered text which are not included in the database.

What I want to do is check first if The referral code inserted by the user exist. But the problem is, it is always going on the addOnSuccessListener even though i had entered text which are not included in the database.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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.sstatic.net/Jr9Vm.png

Upvotes: 1

Views: 835

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Beside the fact that you have a typo in your code referrals.userReferralCOde as is should be referrals.userReferralCode, you aren't getting any Users object from the database because you are creating a new one. You can simply use addOnCompleteListener and you'll see that the QuerySnapshot object contains data that is coming from the database. To see the results, you should iterate, like in the following lines of code:

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);
            }
        }
    }
});

The result in your logcat will be all users that have userReferralCode equal to 123. Please also note that fields in your class do not coresponde with the one from the database. For that, please see my answer from the following post:

So if yo want to get data as an object of Users class, use the recommendations from that answer.

Upvotes: 1

Related Questions