简体   繁体   English

Firebase V9,现有文档添加子集

[英]Firebase V9, add subcollection to existing doc

const q = getDoc(doc(db, "customers", user.uid));

Hello, this returns a promise in the console.log.您好,这会在 console.log 中返回一个 promise。 I have a customers collection and each user.uid is a document inside it.我有一个客户集合,每个 user.uid 都是其中的一个文档。 I want to create a subcollection for each user.uid document, so I can put an object { with some parameters} inside.我想为每个 user.uid 文档创建一个子集合,所以我可以在里面放一个 object {带有一些参数}。 But how do I create a subcollection called "checkout_sessions" for each document(user.uid).但是我如何为每个文档(user.uid)创建一个名为“checkout_sessions”的子集合。 Any help would be appreciated, please, thanks任何帮助将不胜感激,请,谢谢

This is how its done in Firebase v8.这就是它在 Firebase v8 中的完成方式。 by the time of writing the lines the firestore doesnt have a collection called checkout_sessions and this seems to create 1 for each user.uid在编写这些行时,firestore 没有一个名为 checkout_sessions 的集合,这似乎为每个 user.uid 创建了 1 个

const docRef = await db
.collection("customers")
.doc(user.uid)
.collection("checkout_sessions").
add({
 price: priceId,
 and: two,
 more: pairs,
});

I need to create this same subcollection "checkout_sessions" and add data inside in firebase v9, please thank you!!!!!我需要创建相同的子集合“checkout_sessions”并在 firebase v9 中添加数据,谢谢!!!!

Just following up on Frank's answer.只是跟进弗兰克的回答。 If you'd like to use setDoc to set a specific id -如果您想使用 setDoc 设置特定的 id -

const docRef = doc(db, "customers", user.uid, "checkout_sessions", "custom id");
setDoc(docRef, {
    price: priceId,
    and: two,
    more: pairs,
 });

Rewriting code to v9 is typically a matter of changing the method calls to the equivalent function calls, and passing in the object as the first parameter.将代码重写为 v9 通常是将方法调用更改为等效的 function 调用,并将 object 作为第一个参数传入。

In your code snippet that'd be:在您的代码片段中,这将是:

const docRef = doc(db, "customers", user.uid);
const colRef = collection(docRef, "checkout_sessions")
addDoc(colRef, {
 price: priceId,
 and: two,
 more: pairs,
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM