Thuta Learning
Firebase Tutorial
IntermediateDevOps & Toolsintermediate

Data တစ်ခုတည်း ဖတ်ခြင်း

Data တစ်ခုတည်း ဖတ်ခြင်း

Document ID ကိုသိပြီးသားဆိုရင် getDoc နဲ့ document တစ်ခုကိုဖတ်နိုင်ပါတယ်။ User profile, note detail page, product detail page တို့မှာ အများဆုံးသုံးပါတယ်။

Code Example

javascript
import { getFirestore, doc, getDoc } from 'firebase/firestore';

const db = getFirestore();

async function readNote(noteId) {
  const noteRef = doc(db, 'notes', noteId);
  const snapshot = await getDoc(noteRef);

  if (!snapshot.exists()) {
    console.log('Note not found');
    return;
  }

  console.log('Note data:', snapshot.data());
}

readNote('Lk9aPq82xYzExample');

ဒီ code က ဘာလုပ်တာလဲ?

getDoc က document snapshot ကိုပြန်ပေးပါတယ်။ Data တကယ်ရှိ/မရှိကို snapshot.exists() နဲ့စစ်ပြီးမှ snapshot.data() ကိုသုံးတာက safe ဖြစ်ပါတယ်။

Common mistake

Document မရှိတဲ့အချိန်မှာ snapshot.data() ကိုတန်းသုံးပြီး UI ထဲမှာ undefined error ဖြစ်တတ်ပါတယ်။ Not found state ကို သေချာပြပါ။

Data တစ်ခုတည်း ဖတ်ခြင်း | Thuta Learning