简体   繁体   English

获取Firestore的UID参考

[英]Getting UID Reference for Firestore

I'm trying to get a reference to a document's UID so I can use the document elsewhere in my project by it's UID. 我正在尝试获取对文档的UID的引用,因此我可以通过它的UID在我的项目中的其他位置使用该文档。 As per the documentation , I'm doing the following with AngularFire2 (v5): 根据文档 ,我正在使用AngularFire2(v5)执行以下操作:

var ref= this.afs.collection(placeToAdd).doc();

This gives me the following error: "Expected 1 arguments but got 0" . 这给了我以下错误: “预期1个参数但得到0” It wants me to put an argument in the doc() method. 它希望我在doc()方法中加入一个参数。 So instead, I do: 所以相反,我这样做:

var ref= this.afs.collection(placeToAdd).doc(thing);

then I set my Firebase object: 然后我设置我的Firebase对象:

ref.set({
        item1: 'value 1',
        item2: 'value 2',
        item3: 'value 3'
      });

How do I get the ref's Firestore-generated UID now? 我如何获得ref的Firestore生成的UID?

Figured it out: 弄清楚了:

let newUID = this.afs.createId();

Then create this firestore object at the location of newUID: 然后在newUID的位置创建这个firestore对象:

var ref= this.afs.collection(placeToAdd).doc(thing);

To clarify: there is two ways of inserting data in firestore. 澄清一下:有两种方法可以在firestore中插入数据。 One recieves the uid from you. 一个人收到了你的uid。

db.collection(collection).doc(uid).set(data);

The other is adding the thocument but letting firestore generate the uid for you. 另一个是添加thocument但让firestore为你生成uid。

db.collection(collection).add(documentData)
.then(ref => {
    console.log("Document written with ID: ", ref.id);
});

It's important to note that both set and add return a promise of document ref, in case you want to use it and for error handling. 重要的是要注意set和add都会返回文档引用的承诺,以防您想要使用它并进行错误处理。

The documentation covers this in much more detail. 该文档更详细地介绍了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过从其他服务获取UID来在服务中设置Firestore参考异步? - How to set a firestore reference async in service depening on getting UID from other service? 从Firestore获取数据时,如何创建使用用户uid的服务? 角/消防站 - How to create a service for using the user uid when getting data from firestore? angular/firestore Firestore 参考 - Firestore Reference Angular 5,Firestore,OAuth - 在组件中获取auth uid - Angular 5, Firestore, OAuth - get auth uid in component 有没有办法使用 currentUser uid 创建 Firestore 文档 - Is there a way to create a Firestore Document using currentUser uid 为Firestore创建的每个用户的自定义UID - Custom UID for each user created for Firestore AngularFire2获取UID并在任何页面中使用 - AngularFire2 getting UID and using in any page 我们如何从我们在 Firestore 安全规则中使用的 angular firestore 传递 uid - How we can pass uid from angular firestore that we use in firestore security rules 如何使用 Angular Fire 从我的 Angular 代码中检索插入到 FireStore 集合中的文档的 UID? - How to retrieve the UID of a document inserted into a FireStore collection from my Angular code using Angular Fire? 在Firestore中创建结构并获取数据 - creating structure and getting data in Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM