【问题标题】:Angularfire Update nested dataAngularfire 更新嵌套数据
【发布时间】:2018-10-12 16:11:36
【问题描述】:

我正在使用 angularfire 和 firebase firestore 数据库将图像数据保存在数据库中。节省 100%。

我的数据是这样嵌套的:

图片集

  • 画廊 ID
    • 图像ID
      • 姓名
      • 路径
    • 图像ID
      • 姓名
      • 路径
  • 画廊 ID
    • 图像ID
      • 姓名
      • 路径

有时我需要更新数据库中的特定图像对象,但是,当我尝试更新图像对象/文档时,我收到以下错误:

Invalid document reference. Document references must have an even number of segments, but images/yuEXbpYWqE4AUfmRCxIu/JvLzEANRFlupuUxYXNii has 3

这是我使用的代码:

this.afs.doc('images'+'/'+galleryId+'/'+imageId+'/').update({"name": upload.name,"path": upload.url});

我也试过了:

this.afs.doc('images').collection(galleryId).doc(imageId).update({"name": upload.name,"path": upload.url});

我也试过了:

 this.afs.collection('images'+'/'+galleryId).doc(imageId).update({"name": upload.name,"path": upload.url});

然后我得到一个类似的错误,这次只是引用了集合:

 Invalid collection reference. Collection references must have an odd number of segments, but images/AHcWODRgsespIExQnJae has 2

当我尝试更新数据库中的其他集合时,它工作正常,但是,它们只嵌套了 2 层。似乎当您嵌套了 3 个级别时就会出现问题。

请帮忙

【问题讨论】:

    标签: firebase angularfire2 angularfire5


    【解决方案1】:

    您不能将一个文档直接嵌套在另一个文档下。您必须相应地构建您的嵌套:集合 - 文档 - 集合 - 文档 - 等等。Firestore 路径始终以集合开头。

    this.afs.doc('images/'+galleryId + '/images/' + imageId).update({"name": upload.name,"path": upload.url});
    

    我建议阅读 firestore 文档,因为这是 firestore 的基本方面:https://firebase.google.com/docs/firestore/

    【讨论】:

    • 谢谢,我会试试这个并提供反馈!
    • @Janpan,运气好吗?
    • 好的,谢谢。它工作得很好。我有点走神了,因为我重组了所有嵌套数据。感谢您分享文档,我现在理解得更好了,因为我来自实时数据库 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2018-07-13
    • 2020-02-08
    • 1970-01-01
    • 2020-04-15
    • 2015-11-15
    • 2013-11-05
    相关资源
    最近更新 更多