James gonzales
James gonzales

Reputation: 35

The order wont appear when getting a specific value in a collection in firestore

The project I'm working on a E-commerce app and I'm trying to show the list of orders that the user add in a recyclerview

the Problem I'm having is that all the details of the user appear except the orders that the user added doesn't show up and no error appear when I run the application

The collection of the User detail and Item that is been ordered

enter image description here

enter image description here

The code that I use

   private void orderList() {
        final List<List<DocumentSnapshot>> productOrder = new ArrayList<>();

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        CollectionReference userCartOrder = db.collection("UserOrder").document().collection("cartID");
        CollectionReference userCartID = db.collection("ItemOrder");

        Query query = userCartID.whereEqualTo("cartID",userCartOrder);

        query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()){
                    for (QueryDocumentSnapshot documentSnapshot: task.getResult()){
                        CollectionReference cartOrderID = documentSnapshot.getReference().collection("itemList");

                        cartOrderID.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()){
                                    productOrder.add(task.getResult().getDocuments());
                                    

                                }
                            }
                        });
                    }

                }
                else {
                    Toast.makeText(AdminOrderActivity.this, "Query failed", Toast.LENGTH_SHORT).show();
                }
            }
        });




    }

Upvotes: 0

Views: 51

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

The problem is like there:

CollectionReference userCartOrder = db.collection("UserOrder").document().collection("cartID");

When you call document() without any parameter, it generates a reference to a new, unique, non-existing document. Passing that to a query, will not return any orders, as the document doesn't exist.

If you want to query the ItemOrder of a specific cartID, you will need to pass that cart ID to the query. For example:

Query query = userCartID.whereEqualTo("cartID", "220225073844244");

Upvotes: 1

Related Questions