【问题标题】:Add a subcollection without knowing the document ID在不知道文档 ID 的情况下添加子集合
【发布时间】:2021-03-29 05:09:10
【问题描述】:

在 Firebase 中,我有一个集合(“users”),其中包含多个文档。文档 ID 是自动生成的,我想在其中一个文档中添加一个子集合。我已经查询了文档 ID,但不知道下一步我应该怎么做才能正确添加子集合。 enter image description here

【问题讨论】:

    标签: firebase


    【解决方案1】:

    请将您的代码发布为文本,而不是图像。 对于您要执行的操作,您需要使用:

    firebase.firestore().collection('users').doc(InsertYourDocId).collection(nameOf sucollectionYouwantToCreateOrRead).add({'title':title.value})
    

    【讨论】:

    • 谢谢。我之前尝试插入 doc.id,但总是得到 firebase.firestore... 不是功能错误。我分配了 var id = doc.id ,然后将 id 插入 firebase.firestore().collection('users').doc(id).collection("name").... 然后就可以了。
    【解决方案2】:

    试试这个:

    firebase.firestore().collection('users').where("name", "==", "ros corn" ).get().then(snapshot=>{
        const collectionId = snapshot?.docs[0]?.id
        firebase.firestore().collection('users').doc(collectionId). add({
    // Add what you want here
    }).finally(() =>console.log(`Added`) ) 
    } )
    

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 2017-07-28
      • 2013-03-05
      • 2011-10-16
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      相关资源
      最近更新 更多