# STEP 1 :
const db = firebase.firestore()
# STEP 2 :
- 1. Connect to Collection
const collectionRef = db.collection("users");
RETURNS
collectionReference
subset of queryReference
- Not sure why but some how this is NOT an async opertation
NOTE : Now we will only talk about
collectionReference
object notdocumentRefernce
// collectionReference have few methods to get the snapshot
collectionRef.get();
NOTE :
- All the above operations are
async
operations- All the collectionReference operations RETURN
querySnapshot
docs: An array that returns all of the documents in the snapshot
// querySnapshot has docs methods
collectionRef //Reference from db.collection("users")
.get() // Get method to get query Snapshot
.then((snapshot) => { // snapshot has docs property
const data = snapshot.docs.map((doc) => ({ // which returns array
id: doc.id,
...doc.data(), // of DocumentSnapshots, add data() to get JSON
}));
- RECAP :
.data() | .exists
πΊ
DocumentSnapshots
πΊ
.docs()
πΊ
querySnapshot
πΊ
.get()
πΊ
collectionReference
πΊ
.collection()
(OR)
- 2. Connect to Document
const docRef = db.doc("/users/10")
RETURNS
documentReference
subset of queryReference
- Not sure why but some how this is NOT an async opertation
NOTE : Now we will only talk about
documentRefernce
object notcollectionReference
// documentReference have few methods to get the snapshot
docRef.get();
docRef.set();
docRef.update();
docRef.delete();
NOTE :
- All the above operations are
async
operations- All the documentReference operations RETURN
documentSnapshot
# DocumentSnapshot :
Document snapshot have two important methods
- .exists
- .data()
- RECAP :
.data() | .exists
πΊ
DocumentSnapshot
πΊ
.get() | .set() | .update() | .delete()
πΊ
documentRefernce
πΊ
.docs()
===============
Leverage Realtime
- Instead of promise, it's a subscription
use
.onSnapshot(cb)
on collection reference
- Whenever data changes, firestore calls the callback with querySnapshot
(snapshot) => {
const data = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})
Top comments (0)