首页 > 解决方案 > 如何在 AngularFire Firestore 中获取使用 .add() 创建的文档的 ID?

问题描述

我有这个代码

 this.angularFirestore.collection<Loja>("lojas").add(loja)
    .then(data => {
        console.log(data);
     });

我需要获取最近创建文档的文档 ID。我无法从数据变量中获取它...

标签: javascriptfirebasegoogle-cloud-firestoreangularfire2

解决方案


您需要将文档 ID 的创建与文档的编写分开:

const id = this. angularFirestore.createId();
this.angularFirestore.collection<Loja>("lojas").doc(id).set(loja)

另见:


推荐阅读