Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8883

Firestoreのエラー "Converting circular structure to JSON Firebase"

$
0
0

エラーの原因

document内で, referenceタイプのフィールドを使っている場合に発生します.

筆者のアプローチ

referenceタイプのフィールドが他のドキュメントを指しているので, そのドキュメントのパスを文字列型に変換してあげればいいと思います.
他の方法があればご教授いただきたいです.🙇‍♂️

具体的な方法

下記は取得したいドキュメントの DocumentReferenceを引数として与えてあげるとそのドキュメントをオブジェクトとして返してあげる関数です.

constresolveCirculations=(documentRef)=>{constdata=documentRef.data();constcirclerRefResolved=Object.keys(data).reduce((ret,key)=>{ret[key]=has(data[key],'path')?data[key].path:data[key];returnret;},{});return{...circlerRefResolved,id:documentRef.id};};

ドキュメントの一件取得は下記のように使ってあげます.

constpostRef=awaitdb.collection('posts').doc(id).get();constpost=resolveCirculations(postRef);

ドキュメントを複数(コレクションで)取得したい場合は以下で, CollectionReference用に関数を用意してあげます.先程のresolveCirculationsを使います.

constgetDocs=(collectionRef)=>collectionRef.docs.map(resolveCirculations);

上記を使うと, 複数件は以下で取得できます.

constusersRef=awaitdb.collection('users').get();constusers=getDocs(usersRef);

以上になります.


Viewing all articles
Browse latest Browse all 8883

Trending Articles