【发布时间】:2020-01-12 20:26:17
【问题描述】:
我正在创建一个待办事项列表,我想让用户在列表中添加任务。但是,我在获取文档 ID 时遇到了麻烦。
这是我尝试过的两种方法:
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")
不幸的是,这种方式创建了一个新文档而不是将任务存储到列表中。
另一种方法是:
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")
但它说 listId 是未定义的。
这是我的火力基地结构:
User(Collection) - User1...
|
List(Collection) - List1...
|
(Expected)Task(Collection) - task1...
艾米的建议?非常感谢!
【问题讨论】:
-
您需要知道要添加任务的文档的 ID。一旦你,将它传递给
doc():this.list = firebase.firestore().collection(collectionPath).doc(id) -
我在获取文档 ID 时遇到了困难。
-
@Andrewsuen 你试过我的建议了吗?
标签: javascript firebase react-native google-cloud-firestore