将ID保存到Firestore文档 [英] Save id to Firestore document

查看:105
本文介绍了将ID保存到Firestore文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也想将文档的ID另存为一个属性,而不仅仅是作为集合的引用.目前,我将其保存为:

I'd like to save the id of my document as a property as well, not only as a reference on the collection. At the moment I save it like this:

const newSet: AngularFirestoreDocument<SetModel> = this.AngularFirestore.doc('users/' + this.navParams.data.userId);

    // adding user to our ddbb
    newSet.collection('sets').add(this.set)
      .then(function (docRef) {
        // ok
      })
      .catch(function (error) {
        // error
      });

那有可能吗?还是我需要保存它,获取ID并再次更新?

Would be that possible? Or do I need to save it, get the id and update it again?

PS,这是我的ddbb结构:

PS this is my ddbb structure:

推荐答案

通过快速扫描 CollectionReference.add(...) 的参考文档,我看到add(...)返回了一个Promise.在创建文档之前,似乎没有办法获取自动生成的ID.

From quickly scanning the reference documentation for CollectionReference.add(...) I see that add(...) returns a promise. It seems there is no way to get the auto-generated ID before the document is created.

但是请记住,文档ID只是客户端密钥,从统计上讲是唯一的.

But keep in mind that the document IDs are just client-side keys that are statistically guaranteed to be unique.

代码显示了JavaScript SDK生成ID的方式,该ID归结为调用

This code shows what the JavaScript SDK does to generate the ID, which just boils down to calling AutoId.newId(). You can also call (or include in case it isn't publicly exported) this in your own code, and then use doc(myId) instead of add().

这篇关于将ID保存到Firestore文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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