angular - 如何使用 Angular Fire 从我的 Angular 代码中检索插入到 FireStore 集合中的文档的 UID?
问题描述
我正在开发一个使用 AngularFire 与 FireStore 数据库交互的 Angular 9 项目。
我有这个代码片段可以在我的 FireStore 数据库集合上正确执行插入,它工作正常:
this.db
.collection("calendar")
.add({ title: newEvent.title,
start: firebase.firestore.Timestamp.fromDate(newEvent.start),
end: firebase.firestore.Timestamp.fromDate(newEvent.end)
})
.then(function() {
console.log(" event successfully written!");
})
.catch(function(error) {
console.error("Error writing document event: ", error);
});
我只有一个问题。我想检索与插入文档相关的文档UID 。我认为我必须将此行为实现到定义到then()运算符中的函数中(我不完全确定这个断言)但是如何?我不明白实现这种行为的正确方法是什么,也许我遗漏了一些东西。
如何检索此代码插入的新文档的 UID?
解决方案
该add()
方法返回“一个 PromiseDocumentReference
在写入后端后指向新创建的文档。”
因此,以下应该可以解决问题:
this.db
.collection("calendar")
.add({ title: newEvent.title,
start: firebase.firestore.Timestamp.fromDate(newEvent.start),
end: firebase.firestore.Timestamp.fromDate(newEvent.end)
})
.then(function(docRef) { // <-----
const docID = docRef.id; // <-----
console.log(" event successfully written!");
})
.catch(function(error) {
console.error("Error writing document event: ", error);
});
推荐阅读
- html - 如何根据剩余的视口空间制作 div 宽度
- javascript - 在容器内更改焦点时不正确的聚焦触发
- c++ - 如何关联 PCI 卡总线/功能或 VENDEV 和从 Linux C/C++ 安装的 /dev/ttySn
- php - 用 Intervention\Image 函数替换 GD 库函数
- flutter - 自定义抽屉图标颤动
- python - 在 pycharm IDE 中,如何使用脚本文件运行参数化函数
- azure - 外部数据源对数据库范围凭据的未解析引用
- c# - 部署到设备时出现 Xamarin.iOS 错误
- r - RUVadj Bioconductor 包 - if (temp[[4]] > 0) df = df + temp[[4]] else df = Inf 中的错误 - 错误是什么意思,我该如何解决?
- flutter - 如何在颤动中为标签栏下划线添加渐变?