Firestore 引用创建“TypeError:将循环结构转换为 JSON”

Mic*_*dek 4 firebase firebase-realtime-database vuefire google-cloud-firestore

我在 Vue JS 应用程序中使用 Firebase 的 Firestore:

"firebase": "^5.8.0",
"vue-firestore": "^0.3.16",
Run Code Online (Sandbox Code Playgroud)

当我尝试获取具有引用另一个文档(Firestore 中的引用类型)的字段的文档时,出现以下错误:

[Vue warn]: Error in render: "TypeError: Converting circular structure to JSON"
Run Code Online (Sandbox Code Playgroud)

每当我将文档中该字段的类型更改为字符串时,它似乎都可以正常工作。

我知道这是因为 Firestore JS SDK 中的某些内容试图将文档(以及文档附带的一堆元数据)序列化为 JSON,并且某处有循环引用?

在我的数据结构和字段中,我没有循环引用。它只是一个引用另一个文档的字段,并且被引用的文档不再引用任何其他文档。

我获取数据的代码是:

methods: {
      getContent() {
        const db = this.$firebase.firestore();
        db
          .collection('places')
          .doc(this.$route.params.placeKey)
          .orderBy('name')
          .get()
          .then(snap => {
            this.places = []

            snap.forEach(doc => {
              this.places.push(doc.data())
            })
          })
      }
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:

1)首先,我在代码中做错了什么吗?即使没有引发错误,Firestore 的 JS SDK 会为我解析参考吗?或者我是否必须调用参考并自己解决以获取参考文档的数据?2) 当您有可以参考的文档时,Firestore 的最佳做法是什么?你应该使用引用吗?什么时候?你什么时候会非规范化?

谢谢!

Ren*_*nec 6

回答关于存储引用的第二个问题:实际上,在撰写此答案时,将引用(即路径元素)存储为类型而不是将它们存储为字符串并没有真正的优势Reference

请观看来自 Firebase https://www.youtube.com/watch?v=Elg2zDVIcLo&t=274s 的官方视频,其中包含详细说明(从 4:34 开始)。