Javascript firestore中的引用类型
我正在尝试在firestore中实现documentreference。我创建了两个集合:Javascript firestore中的引用类型,javascript,firebase,react-native,google-cloud-firestore,Javascript,Firebase,React Native,Google Cloud Firestore,我正在尝试在firestore中实现documentreference。我创建了两个集合:客户和购买的财产。每次在Purchased-Property集合中创建新文档时,如何在Purchased-Property\u Customer\u id中引用Customer\u id。 代码如下所示: const CustomerDoc = firebase.firestore().collection("Customers").doc() CustomerDoc.set({
客户
和购买的财产
。每次在Purchased-Property
集合中创建新文档时,如何在Purchased-Property\u Customer\u id
中引用Customer\u 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
})
CustomerDoc
是一个对象。每个DocumentReference都有一个id
属性。您可以这样简单地使用它:
PurchasedPropertyDoc.set({
Purchased_Property_id: PurchasedPropertyDoc.id,
Purchased_Property_Name: Property_Name,
Purchased_Property_Location: Property_Location,
Purchased_Property_Customer_Id: CustomerDoc.id
})
这与您在第一个文档中首次使用
CustomerDoc.id
的方式没有什么不同。如果您在创建文档时没有此引用,那么您将无法建立关联。那么我如何使用引用数据类型来更方便地定位客户Id
,因为上面的答案是正确的,所以使用引用类型将其存储为字符串并不一定简单。您仍然需要查询另一个文档。但如果需要引用,您可以直接传递CustomerDoc
。