1

I am creating a todo-list, and I want to enable users to add the task in the list. However, I am troubled in getting document id.

Here are 2 methods I have tried:

 this.currentUser = firebase.auth().currentUser;
        const currentUserUid = this.currentUser.uid;
        let collectionPath = "/userProfile/" + currentUserUid  + "/list";

        this.list = firebase.firestore().collection(collectionPath).doc()
        const listId = this.list.id; 

        this.ref = firebase.firestore().collection(collectionPath)
                  .doc(listId).collection("task")

Unlucky, this way createe a new document insteading of storing task into the list.

Another method is:

        this.currentUser = firebase.auth().currentUser;
        const currentUserUid = this.currentUser.uid;
        let collectionPath = "/userProfile/" + currentUserUid  + "/list";

        this.list = firebase.firestore().collection(collectionPath)
                   .get().then((snapshot) => {
                   snapshot.docs.forEach(doc => {
                       return doc.data();                    
                   })
                   })
        const listId = this.list.id; 

        this.ref = firebase.firestore().collection(collectionPath)
                  .doc(listId).collection("task")

But it said listId is undefined.

Here is my firebase structure:

User(Collection) - User1...
                     |
                   List(Collection) - List1...
                                        |
                             (Expected)Task(Collection) - task1...

Amy suggestions? Thanks a lot!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

In order to get the document's ID I would suggest you take a look at this answer. Also maybe this may prove helpful. As for adding a subcollection to a document I would suggest you reading this question and the following documentation.

Let me know if these were helpful.

tzovourn
  • 1,293
  • 8
  • 18