skip to Main Content

In DB I have a collection called Users, in this case the global one, then comes the document referring to each user, inside there are separations according to what is necessary.

FireStore img

In my case, the userData was created, but there would be other collections such as pantry, list, etc.

My question is, how can I make the creation of these subcollections of mine go according to the user’s document?

I try this:

private void show() {
    DocumentReference docRef = db.collection("users").document(auth.getUid()).collection("myPentry").document("FQpG5QWFiJ4xStsiDING");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    String banco = document.getString("description");
                    edPantry.setText(banco);
                    edPantry.setSelection(edPantry.getText().length());
                } else {
                    Log.d("n", "No such document");
                }
            } else {
                Log.d("erro", "get failed with ", task.getException());
            }
        }
    });
}

But the auth.getUid() return null object reference

2

Answers


  1. Chosen as BEST ANSWER

    I solved my problem as follows.

    I separated the user data (email, name) for a totally different collection. The data that is generated when you are logged in are in another collection.

    That way it manages to capture the user's id and place it as the document, doing the segregation I would like and organizing the internal information in the respective collections


  2. There is no API to create (or delete) a collection in Firestore. Instead, a collection is automatically created when you write the first document, and conversely automatically removed when you remove the last document from it.

    So if you want to create multiple subcollections under the user document, you’ll have to write a document to each of them similar to what you already do for userData.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search