[英]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.