如何检索刚刚插入的 firestore 文档的 id(使用 angularfire2)? [英] How to retrieve id of just inserted firestore document (with angularfire2)?

查看:27
本文介绍了如何检索刚刚插入的 firestore 文档的 id(使用 angularfire2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个新文档插入到我的 firestore 集合中,如下所示:

I'm inserting a new document into my firestore collection like so:

this.afs.collection<Client>('clients').add(this.form.value).then(docRef => {
  console.log("Need to output the firestore generated doc id of the document here: ...")
})

推荐答案

明白了:then() 采用 联合类型 (http://www.typescriptlang.org/docs/handbook/advanced-types.html): void |文档参考.

Got it: then() takes a union type (http://www.typescriptlang.org/docs/handbook/advanced-types.html): void | DocumentReference.

因此需要这样处理:

this.afs.collection<Client>('clients').add(this.form.value).then(docRef => {
  console.log((docRef) ? (<DocumentReference>docRef).id : 'void') // docRef of type void | DocumentReference
})

这篇关于如何检索刚刚插入的 firestore 文档的 id(使用 angularfire2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆