[英]Can I construct a sub-collection? (Firestore)
I'm trying to figure out how to construct a new sub-collection (RED CIRCLE) inside the following directory in Firestore:我试图弄清楚如何在 Firestore 的以下目录中构建一个新的子集合(红色圆圈):
directory: "courses/{userID}/{courseID} "目录:“课程/{userID}/{courseID}”
with the courseID to be randomly generated ID upon creation of the sub-collection inside the "userID" document. courseID 是在“userID”文档中创建子集合时随机生成的 ID。 Unfortunately the code I was able to get working, using the.set(), doesn't create a new sub-collection, but rather just updates the current document.
不幸的是,我能够使用 the.set() 开始工作的代码不会创建新的子集合,而只是更新当前文档。
Is there a function that I can use to create a new collection inside the document?我可以使用 function 在文档中创建新集合吗?
CODE:代码:
export const createCourse = (course) => {
(...)
//firestore.collection("courses").doc(authorId).set({ <---This commented out line updates the document, yet I'm trying to construct a new sub-collection inside this document, not just change fields inside of it.
firestore.collection("courses").doc(authorId).add({ <--- This gives me an error stating " firestore.collection(...).doc(...).add is not a function ". I am trying to figure out what is the correct syntax for this intended action.
...course,
authorUID: authorId,
courseCreatedAt: new Date()
}).then(() => {
(...)
};
There is no API for what you're trying to do.您正在尝试做的事情没有 API 。 Also, it's generally not a good idea to use random collection name to model your data.
此外,对 model 数据使用随机集合名称通常不是一个好主意。 Collections names should be known by your app ahead of time, because:
您的应用程序应该提前知道 Collections 名称,因为:
Consider instead using a subcollection named "courses", then add a document under there with a random ID.考虑改为使用名为“课程”的子集合,然后在下面添加一个带有随机 ID 的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.