我正在尝试在firestore中实现documentreference。我创建了两个集合:Customers
和Purchased-Property
。如何在每次在Purchased-Property
集合中创建新文档时引用Purchased-Property_Customer_id
中的Customer_id
?代码如下所示:
const CustomerDoc = firebase.firestore().collection("Customers").doc()
CustomerDoc.set({
Customer_id: CustomerDoc.id,
Customer_Name: Name
Customer_PhoneNumber: PhoneNumber
})
const PurchasedPropertyDoc = firebase.firestore().collection("Purchased-Property").doc()
PurchasedPropertyDoc.set({
Purchased_Property_id: PurchasedPropertyDoc.id,
Purchased_Property_Name: Property_Name,
Purchased_Property_Location: Property_Location,
Purchased_Property_Customer_Id: Customer_Id //How do i make reference to this Customer_Id in the Customers collection everytime a new document under the Purchased-Property collection is made
})
2条答案
按热度按时间dluptydi1#
CustomerDoc
是DocumentReference对象。每个DocumentReference都有一个id
属性。你可以这样简单地使用它:这与您在第一个文档中第一次使用
CustomerDoc.id
的方式没有什么不同。如果在创建文档时没有此引用,则无法进行关联。olmpazwi2#
你必须创建一个ref,就像:
例如:const CostumerRef = db.collection(“users”).doc(userId),然后设置为Purchased_Property_Customer_Id:CostumerRef