Firestoreからデータを取得する際の基本知識
まず覚えておきたいこと
collectionReference
基本はcollectionに含まれるsubcollectionやdocumentを取得するので
collectionの取得方法
const collectionRef = db
.collection('hoge')条件に該当するのすべて取得する系
これがQuery
const query = db
.collection('hoge')
.where('key','==','aaa')これがQuerySnapshot
const querySnapshot = query.get()これがQueryDocumentSnapshot
const queryDocumentSnapshot = querySnaphot.docs取れた中身は
[
QueryDocumentSnapshot,
QueryDocumentSnapshot,
QueryDocumentSnapshot...
]という感じの構成になっている
これがDocumentData(ただのobject)
const documentData = queryDocumentSnapshot[0].data{
name: 'hoge_content',
key: 'aaa'
}id指定で狙い撃ちの取得する系
これがdocumentReference
const documentRef = collectionRef.doc('moge')これがdocumentSnapshot
const documentSnapshot = documentRef.get()これがDocumentData
const documentData = documentSnapshot.data()ちなみにこれもDocumentReference
const documentRef = documentSnapshot.ref愛の水中花みたいになっちゃった