从Firestore集合(Android)检索文档ID [英] Retrieving Document Id from Firestore Collection (Android)

查看:44
本文介绍了从Firestore集合(Android)检索文档ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取文档下的自动生成的ID,以便可以在其他地方使用.

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

这是完整的代码:

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);

                }
            });

如您所见,我正在使用下面显示的代码生成一个名为"my-chats"的集合,而.document()正在创建自动生成的文档ID.

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>() {

然后,我使用下面显示的代码行尝试从该文档中获取ID.

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();

最后使用下面的代码行,我试图将其放入我创建的HashMap中.

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

myChatFields.put("chatCardId", chatId);

我遇到两个主要问题:

1)我用来提取文档ID的代码行不起作用,并且正在提取其他一些新的自动生成的ID(我猜这是因为我之前使用的是.document()方法.getId()方法).

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)同样由于某些原因,信息也不会随我输入的最后一行代码一起添加到HashMap中.

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:

数据库图片

我正在尝试检索"1"并将其添加到"2"区域.

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

推荐答案

1)我用来提取文档ID的代码行不起作用,并且正在提取其他一些新的自动生成的ID(我猜这是因为我之前使用的是.document()方法.getId()方法).

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).

不,这是因为您正在调用CollectionReference的 document()方法:

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

返回文档引用,该文档引用指向此集合中具有自动生成的ID的文档.

因此,每次您调用 document()方法时,都会生成一个新的新ID.要解决此问题,请更改以下代码行:

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(/* ... */);

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

看,我使用了第一次在引用内部生成的 id .现在,在 onSuccess()方法中,您应该使用与上面生成的相同的 id :

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)同样由于某些原因,信息也不会随我输入的最后一行代码一起添加到HashMap中.

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

这是在您使用错误的引用的情况下发生的.使用包含正确的 id 的引用将解决您的问题.

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

这篇关于从Firestore集合(Android)检索文档ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆