1

I am creating a following system with Firestore using the following schema:

 --- following (collection)
   |      |
   |      --- uid (document)
   |           |
   |           --- userFollowing (collection)
   |                 |
   |                 --- uid (documents)
   |                 |
   |                 --- uid (documents)

But I can't put the documents (the user ids) in the collection. This code:

Map<String, Object> following = new HashMap<>();
                    following.put("title", uid);

                    mDocRef = fsRef.collection("following").document(fAuth.getCurrentUser().getUid()).collection("userFollowing").document(uid);
                    mDocRef.set(following).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            followornot.setText("Segui giá");
                            followornot.setBackgroundColor(Color.argb(255,198, 198, 198));
                            followornotBool = true;
                        }
                    });

produces this result in the Firestore:

enter image description here

if I try with:

        mDocRef = fsRef.collection("following").document(fAuth.getCurrentUser().getUid()).collection("userFollowing");
        mDocRef.set(following).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                followornot.setText("Segui giá");
                followornot.setBackgroundColor(Color.argb(255,198, 198, 198));
                followornotBool = true;
            }
        });

It will give me the following error:

Required type: DocumentReference 
Provided: CollectionReference

so I can't do it

Is there a way to get the data structure I wrote at the beginning of the post?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Conta
  • 236
  • 1
  • 6
  • 21
  • "produces this result in the firestore" ,yes your result is correct .you have placed your document under "userFollowing" and last column is document fields(title) not collection.@pop home – androidLearner Jun 07 '21 at 16:01

1 Answers1

0

You are getting the following error:

Required type: DocumentReference 
Provided: CollectionReference

Because you are trying to call the "set()" method on an object of type CollectionReference, which is actually not possible. Why? Because "set()" is a method that exists within the DocumentReference class and not within the CollectionReference class, hence the error. However, add() can be used.

The problem in your code lies in the fact that you are using the same UID in both document() method calls. In the second one, you should the UID of the user you are following, not the UID of the authenticated user. So in that collection, you should add only documents of the users you follow. Only then you can call a useful following system. More info below:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193