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

查看:22
本文介绍了将 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(...) 返回一个承诺.在创建文档之前,似乎没有办法获取自动生成的 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.

这个code 显示了 JavaScript SDK 为生成 ID 所做的工作,这归结为调用 AutoId.newId().您也可以在您自己的代码中调用(或在未公开导出的情况下包含)它,然后使用 doc(myId) 而不是 add().

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天全站免登陆