Retrieving Document Id from Firestore Collection (Android)

11,574

Solution 1

1) The line of code I'm using to extract the document Id is not working and is extracting some other new auto-generated id (I'm guessing it is because I'm using the .document() method before the .getId() method).

No, this is happening because you are calling CollectionReference's document() method twice:

Returns a DocumentReference pointing to a new document with an auto-generated ID within this collection.

So every time you are calling document() method, a fresh new id is generated. To solve this, please change the following lines of code:

mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
                .document().set(myChatFields)
                .addOnSuccessListener(/* ... */);

to

String id = mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
                .document().getId();
mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
                .document(id).set(myChatFields)
                .addOnSuccessListener(/* ... */);

See, I have used the id that was generated first time inside the refernce. Now inside the onSuccess() method, you should use the same id that was generated above:

myChatFields.put("chatCardId", id);

And not a newer one, as you do in your actual code.

2) Also the information for some reason does not get added to the HashMap with the last line of code I put.

This is happening becase you are using a wrong reference. Using a reference that contains the correct id will solve your problem.

Solution 2

For part one, you'll want to get the document reference in the onSuccess function vs. void. So that would look something like this -

      mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats").add(myChatFields)
                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
        @Override
        public void onSuccess(DocumentReference documentReference) { 


                String chatId = documentReference.getId();

                Log.v("CHATS", chatId);

                myChatFields.put("chatCardId", chatId);

                Intent goToChatActivity = new Intent(UserProfileActivity.this,
                        ChatActivity.class);
                startActivity(goToChatActivity);

            }
        });
Share:
11,574
Kav
Author by

Kav

Updated on August 21, 2022

Comments

  • Kav
    Kav almost 2 years

    I'm trying to extract the auto-generated Id under a document so I can use it elsewhere.

    Here is the full code:

    mStartChatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                final HashMap<String, Object> myChatFields = new HashMap<>();
                myChatFields.put("dateJoined", ServerValue.TIMESTAMP );
                myChatFields.put("status", "");
                myChatFields.put("snoozeUntil", "");
                myChatFields.put("myCharacter", "");
                myChatFields.put("requestedCustomChatCode", "");
                myChatFields.put("groupChatName", "");
                myChatFields.put("toInviteMembers", "");
                myChatFields.put("lastMessage", "");
                myChatFields.put("lastMessageTimeStamp", ServerValue.TIMESTAMP);
    
                mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
                        .document().set(myChatFields)
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
    
                        String chatId = mWhammyUsersCollection.document(mCurrentUserId)
                                .collection("my-chats").document().getId();
    
                        Log.v("CHATS", chatId);
    
                        myChatFields.put("chatCardId", chatId);
    
                        Intent goToChatActivity = new Intent(UserProfileActivity.this,
                                ChatActivity.class);
                        startActivity(goToChatActivity);
    
                    }
                });
    

    As you can see I'm using the code shown below to generated a Collection called "my-chats" and the .document() is creating the auto-generated document id.

    mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
                        .document().set(myChatFields)
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
    

    Then I'm using the line of code shown below to try get the id from that document.

    String chatId = mWhammyUsersCollection.document(mCurrentUserId)
                                .collection("my-chats").document().getId();
    

    Finally using the line of code below, I'm trying to put it into the HashMap I have created.

    myChatFields.put("chatCardId", chatId);
    

    There are two main problems I am having:

    1) The line of code I'm using to extract the document Id is not working and is extracting some other new auto-generated id (I'm guessing it is because I'm using the .document() method before the .getId() method).

    2) Also the information for some reason does not get added to the HashMap with the last line of code I put.

    How can I solve these two issues?

    To explain it a bit more graphically:

    Picture of database

    I'm trying to retrieve "1" and add it in the area of "2".

  • Kav
    Kav over 5 years
    I tried this but it says "in Task cannot be applied"
  • R. Wright
    R. Wright over 5 years
    Missed the use of set vs. add. I've adjusted above. (Although, I do think they're supposed to be equivalent.)
  • Kav
    Kav over 5 years
    Alright so your correction made the addOnSuccessListener work but now the collection "my-chats" is not being created in the database at all hence, the Log also doesn't show anything.
  • Ran Marciano
    Ran Marciano over 3 years
    Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes