javascript Firestore中的参考类型

yhived7q  于 2023-06-04  发布在  Java
关注(0)|答案(2)|浏览(100)

我正在尝试在firestore中实现documentreference。我创建了两个集合:CustomersPurchased-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 

    
        })
dluptydi

dluptydi1#

CustomerDocDocumentReference对象。每个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的方式没有什么不同。如果在创建文档时没有此引用,则无法进行关联。

olmpazwi

olmpazwi2#

你必须创建一个ref,就像:
例如:const CostumerRef = db.collection(“users”).doc(userId),然后设置为Purchased_Property_Customer_Id:CostumerRef

相关问题