我可以构建一个子集合吗?(消防站)

我试图弄清楚如何在 Firestore 的以下目录中构建一个新的子集合(红色圆圈):

http://img2.mukewang.com/63ad47970001c1f512900730.jpg

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


courseID 是在“userID”文档中创建子集合时随机生成的 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(() => {


    (...)


};


慕村225694
浏览 129回答 1
1回答

POPMUISE

没有针对您要执行的操作的 API。此外,使用随机集合名称对数据建模通常不是一个好主意。您的应用程序应该提前知道集合名称,因为:如果您不知道其名称,则无法查询集合。没有客户端 API 可以列出文档下的子集合。考虑改为使用名为“courses”的子集合,然后在其下添加一个具有随机 ID 的文档。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript