How to change the value of a perticular field (state) of different documents having same uid(one of the field like state) under a collection?

There are two problems with your code. The first is the call of the .document() method without passing any argument, which actually creates a new id. The second problem is that you are not looping to get the result. So to solve this, please use the following lines of code:

placeOrder.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        db.collection("Pending")
                .whereEqualTo("uid", user.getUid())
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                db.collection("Pending").document(document.getId()).update("state", "orders")
                                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                //finish();
                                            }
                                        });
                            }
                        }
                    }
                });
    }
});

Be also aware that calling finish(); will finish the activity when the first update completes, that’s the reason why I commented that line.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top