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!