javascript - 如何在 Firestore 中获取生成的文档 ID,然后将子集合添加到其中?
问题描述
我正在创建一个待办事项列表,并且我想让用户能够在列表中添加任务。但是,我在获取文档 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...
艾米的建议?非常感谢!
解决方案
推荐阅读
- javascript - 打字稿:在同一级别重用 for .. of 和 for .. in
- arrays - Kotlin - 对象数组(不为空)
- python - 在 matplotlib 中更改 datetime.time 轴的格式
- git - 如何使用 nodegit 检测 git 存储库分歧
- c# - 为什么我不能将具体类添加到具体类实现的接口列表中?
- javascript - css过渡高度隐藏容器中的内容
- python - Python3.7:线程和创建目录
- ruby-on-rails - 从另一个自定义验证中调用验证
- math - 计算非重复排列中元素的索引
- python - 使用 Python 调整 HTML 中的图像大小 - 我该如何正确操作?