【问题标题】:Can I construct a sub-collection? (Firestore)我可以构建一个子集合吗? (火库)
【发布时间】:2020-08-10 18:10:12
【问题描述】:

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

目录:“课程/{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(() => {

    (...)

};

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    对于您正在尝试做的事情没有 API。此外,使用随机集合名称来建模数据通常不是一个好主意。您的应用应该提前知道集合名称,因为:

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

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

    【讨论】:

    • 一如既往,感谢 Doug 的澄清和建议! :)
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 2018-12-11
    • 2012-12-20
    • 1970-01-01
    • 2021-10-26
    • 2019-06-28
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多