我可以构造一个子集合吗? Firestore

问题描述

我正在尝试找出如何在Firestore的以下目录内构造新的子集合(RED CIRCLE):

enter image description here

目录:“课程/ {userID} / {courseID}”

在创建“ userID”文档中的子集合时,将使用courseID作为随机生成的ID。不幸的是,我使用.set()能够使用的代码不会创建新的子集合,而只是更新当前文档。

有没有可用于在文档内部创建新集合的函数

代码

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(() => {

    (...)

};

解决方法

您没有要执行的操作的API。另外,使用随机集合名称为数据建模通常也不是一个好主意。您的应用应该提前知道集合名称,因为:

  1. 如果您不知道集合的名称,则无法查询。
  2. 没有客户端API可以列出文档下的子集合。

请考虑使用名为“ courses”的子集合,然后在其中添加带有随机ID的文档。