【问题标题】:how to add sub collection to a document in firestore? [closed]如何将子集合添加到 Firestore 中的文档? [关闭]
【发布时间】:2018-07-30 02:51:19
【问题描述】:

没有关于如何在 firestore 的文档中添加子集合的文档, 如何使用 Web 应用程序添加子集合? even tried like this but didn't worked。 如何使用代码在应用程序中添加子集合? 有什么办法吗,或者使用firebase实时数据库会更好? 如果是这样,请让我知道如何在 firebase 中进行操作i want to add like this i did this 但我收到这样的错误the error says this

【问题讨论】:

  • 解决方案是:获取感兴趣的文档(doc),然后:doc.ref.collection('Collection_Name').add('Object_to_be_added') 建议在承诺。
  • OP 你能在你的问题中加入代码块,而不是代码图像吗?

标签: javascript node.js firebase vue.js google-cloud-firestore


【解决方案1】:

如果您将.set 更改为.add,您的代码应该可以工作。然后,Cloud Firestore 将为该文档颁发一个新 ID。如果要指定文档 ID,则需要执行此操作...

设置具有给定 ID 的图书

db.collection('users').doc(this.username).collection('booksList').doc(myBookId).set({
  password: this.password,
  name: this.name,
  rollno: this.rollno
})

此页面详细介绍了如何Add data to Cloud Firestore

添加一本书

db.collection('users').doc(this.username).collection('booksList').add({
  password: this.password,
  name: this.name,
  rollno: this.rollno
})

【讨论】:

  • 我也试过 add 方法,甚至试过你的代码,但没有用。它显示错误
  • 您需要将错误粘贴到此处。您在屏幕截图中提供的代码太少,无法确定问题所在。正常做法是将代码 sn-ps 粘贴到 Stack Overflow 中,而不是简单地提供截图。
  • 我已经编辑了问题,我添加了显示错误的图片,请先生检查一次。
  • 谢谢,我做错了。我用 set 而不是 add
  • 是的。我刚刚更新了我的答案。你的错误是直到显示你使用set,而不是add。我很高兴你现在把它整理好了
猜你喜欢
  • 1970-01-01
  • 2020-01-05
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 2020-06-28
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多