SHORT GUIDE TO FIRESTORE REFERENCE DATA TYPE

CLOUD FIRESTORE

REFERENCE DATA TYPE IS ONE OF THE MANY DATA TYPES OF CLOUD FIRESTORE . THIS DATA TYPE ACTS LIKE FOREIGNKEY EVEN THOUGH IT IS NOT ACTUALLY A FOREIGNKEY .

THE MAIN ADVANTAGE OF USING IT IS TO REFER A CERTAIN DOCUMENT TO ANOTHER DOCUMENT .

FOR EXAMPLE:

IF WE HAVE A DOCUMENT ‘FIRST FLOOR’ IN COLLECTION OF FLOORS , THEN ALL ROOM DOCUMENTS WHOSE FLOOR IS FIRST FLOOR SHOULD USE REFERENCE DATA TYPE TO DIRECTLY CONNECT TO THE FIRST FLOOR DOCUMENT.

SO NOW HOW TO ACCESS THIS FIELD ??

JUST LET ME GO THROUGH THE HOME PAGE FIRST , WHERE WE DISPLAY THE PARENT COLLECTION…

THIS IS HOW TO GET THE FLOORS FROM FLOORS COLLECTION.

AND NOW HOW TO GET THE ROOMS WHILE VIEWING THE FLOORS DETAIL PAGE..?

DO READ THE GREEN COMMENTS TO UNDERSTAND THE CODE..!

ON DETAILPAGE WE ARE FIRST GETTING ALL DOCUMENTS WHICH HAVE THEIR REFERENCE SET TO THAT PARTICULAR DOCUMENT BY WRITING

db.collection("rooms").where('floor','==',docRef).get().then(
//GET THOSE DOCUMENTS)

FLOOR FIELD IS THE REFERENCE DATA TYPE AND HAS A REFERNCE OF THE FLOOR DOCUMENT .

IMPORTANT : IT CAN ONLY REFER TO ONE DOCUMENT

NOW HOW TO GET DATA FROM THE REFERENCE FIELD??

GETTING DATA FROM REFERENCE FIELD

VERY EASY !! JUST DO LIKE THIS AND YOU MIGHT GET YOUR DESIRED RESULTS..

JUST A CORRECTION:

room.f=res.data();
//ON LINE 4 OF 'GETTING DATA FROM REFERENCE FIELD' IMAGE
//USE ANY NAME EXCEPT THE NAME ALREADY DEFINED

HOPE THIS HELPS !! 😊😊

IF I AM MISTAKEN FEEL FREE TO CORRECT ME 😅😅

Interested in frontend developement & UI design

Interested in frontend developement & UI design