Firestore引用会创建"TypeError:将圆形结构转换为JSON"; [英] Firestore references create a "TypeError: Converting circular structure to JSON"

查看:129
本文介绍了Firestore引用会创建"TypeError:将圆形结构转换为JSON";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Firebase's Firestore inside a Vue JS app:

"firebase": "^5.8.0",
"vue-firestore": "^0.3.16",

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

When I try fetching a document which has a field referencing another document (reference type in Firestore), I get the following error:

[Vue warn]: Error in render: "TypeError: Converting circular structure to JSON"

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

Whenever I change the type of that field in the document to a string, it seems to work fine.

我了解这是由于Firestore JS SDK中的某些内容正在尝试将文档(以及文档随附的一堆元数据)序列化为JSON,并且某处有循环引用?

I understand this is because of something in the Firestore JS SDK that is trying to serialize the document (and a bunch of metadata coming with the document) to JSON and there is a circular reference somewhere?

在我的数据结构和字段中,我没有循环引用.仅仅是引用另一个文档的一个字段,被引用的文档就不再引用任何其他文档.

In my data's structure and fields I do not have a circular reference. It's simply one field that is referencing another document, and the referenced document has no more references to any other document.

我获取数据的代码是:

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())
            })
          })
      }

所以我的问题是:

1)首先,我在代码中做错了什么吗?即使没有出现错误,Firestore的JS SDK也会为我解决该引用吗?还是我必须调用该引用并自己解析才能获得引用文档的数据? 2)当您拥有可以引用的文档时,Firestore的最佳实践是什么?您应该使用参考吗?什么时候?您什么时候可以进行非规范化?

1) First of all, am I doing something incorrectly in my code? Even if there was no error being raised, would Firestore's JS SDK resolve the reference for me? Or would I have to call the reference and resolve it myself to get the referenced document's data? 2) What's the best practice with Firestore when you have documents you can be referenced? Should you use references? When? When would you denormalize?

谢谢!

推荐答案

要回答有关存储引用的第二个问题:实际上,在撰写此答案时,没有将引用(即路径元素)存储为<​​a href="https://firebase.google.com/docs/firestore/manage-data/data-types" rel="nofollow noreferrer"> Reference 类型,而不是将它们存储为String.

To answer to your second question about storing References: actually, at the time of writing this answer, there is no real advantage of storing References (i.e. path elements) as Reference type instead of storing them as String.

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

Please watch this official video from Firebase https://www.youtube.com/watch?v=Elg2zDVIcLo&t=274s which contains detailed explanations (starting at 4:34).

这篇关于Firestore引用会创建"TypeError:将圆形结构转换为JSON";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆