我想在城市集合中获取社会集合的文档 ID,但我的代码在城市集合的 SocietyId 字段中返回不同的 ID。
myId: string = "";
private docRef: firebase.firestore.DocumentReference;
this.docRef = firebase.firestore().collection("societies").doc();
this.myId = this.docRef.id;
this.cityList = firebase.firestore().collection("cities").add({
cityName: this.city,
societyId: this.myId
}).then((doc) => {
console.log(doc);
}).catch((err) => {
console.log(err);
});
您得到了 another societyId
,它与通过简单调用doc()
函数生成的 id 不同,因为您正在使用 CollectionReference 的add()函数向数据库添加一个对象:
将具有指定数据的新文档添加到此集合,并自动为其分配文档 ID。
而不是使用 DocumentReference 的set()函数:
写入此 DocumentReference 引用的文档。如果该文档尚不存在,则会创建它。如果传递选项,则可以将提供的数据合并到现有文档中。
根据您的评论,如果您想拥有相同的id,请更改以下代码行:
this.cityList = firebase.firestore().collection("cities").add(/* ... */);
至
this.cityList = firebase.firestore().collection("cities").doc(this.myId).set(/* ... */);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句