如何使用Angular Fire从我的Angular代码检索插入FireStore集合的文档的UID?

如何使用Angular Fire从我的Angular代码检索插入FireStore集合的文档的UID?,angular,firebase,google-cloud-firestore,angularfire2,Angular,Firebase,Google Cloud Firestore,Angularfire2,我正在从事AngularFire与FireStore数据库交互的Angular9项目 我有一个代码片段,可以正确地在FireStore数据库的集合上执行插入,它工作正常: this.db .collection("calendar") .add({ title: newEvent.title, start: firebase.firestore.Timestamp.fromDate(newEvent.start),

我正在从事AngularFire与FireStore数据库交互的Angular9项目

我有一个代码片段,可以正确地在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?

该方法返回“在将新创建的文档写入后端后,通过指向该文档解决的承诺。”

因此,以下几点可以做到:

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);
        });
this.db
.收藏(“日历”)
.add({title:newEvent.title,
开始:firebase.firestore.Timestamp.fromDate(newEvent.start),
结束:firebase.firestore.Timestamp.fromDate(newEvent.end)
})
.then(function(docRef){//该方法返回“在将新创建的文档写入后端后,使用指向该文档的指针解析的承诺。”

因此,以下几点可以做到:

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);
        });
this.db
.收藏(“日历”)
.add({title:newEvent.title,
开始:firebase.firestore.Timestamp.fromDate(newEvent.start),
结束:firebase.firestore.Timestamp.fromDate(newEvent.end)
})

.then(function(docRef){//FYI AngularFire API与普通JavaScript API不完全相同。我们实际上不知道这里使用的是哪一个。AF将几乎所有内容都封装到自己的类型中。请参阅:感谢您指出@DougStevenson。在
add()的情况下,我们不能从中推断出吗
,它的行为方式将与JavaScript SDK相同?我不知道。我从未使用过它。从
this.db.collection(“calendar”).doc()开始记录生成的doc.id,然后进行设置如何?@danh这确实也是一种可能的方法。我想,谁会在想要生成docRef时使用这种方法呢(以及文档ID)在实际创建文档之前使用,例如,将侦听器设置为要创建的文档,或在批写入中使用文档ID,以便同时创建具有相同文档ID的另一个文档(在另一个集合中).FYI AngularFire API与普通JavaScript API不完全相同。我们实际上不知道这里使用的是哪一个。AF将几乎所有内容都封装到自己的类型中。请参阅:感谢@DougStevenson指出这一点。在
add()的情况下,我们不能从中推断吗
,它的行为方式将与JavaScript SDK相同?我不知道。我从未使用过它。从
this.db.collection(“calendar”).doc()开始记录生成的doc.id,然后进行设置如何?@danh这确实也是一种可能的方法。我想,谁会在想要生成docRef时使用这种方法呢(以及文档ID)在实际创建文档之前使用,例如,将侦听器设置为要创建的文档,或在批写入中使用文档ID,以便同时创建具有相同文档ID的另一个文档(在另一个集合中)。