Thuta Learning
ရှာဖွေရန်
IntermediateDevOps & Toolsintermediate

Data ပြင်ဆင်ခြင်း (Update)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Data ပြင်ဆင်ခြင်း (Update)

updateDoc က document ထဲက field အချို့ကိုပဲပြင်ချင်တဲ့အခါသုံးပါတယ်။ Note title ပြင်ခြင်း၊ pinned state ပြောင်းခြင်း၊ profile name ပြင်ခြင်းတို့မှာ အသုံးဝင်ပါတယ်။

Code Example

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

const db = getFirestore();

async function updateNote(noteId, newTitle, newContent) {
  const noteRef = doc(db, 'notes', noteId);

  await updateDoc(noteRef, {
    title: newTitle,
    content: newContent,
    updatedAt: serverTimestamp()
  });

  console.log('Note updated');
}

ဘာကြောင့် updateDoc သုံးတာလဲ?

Document တစ်ခုလုံးကို အစားထိုးချင်တာမဟုတ်ဘဲ field အချို့ကိုပဲပြင်ချင်လို့ပါ။ ရှိပြီးသား ownerId, createdAt စတဲ့ field တွေကို မထိဘဲထားနိုင်ပါတယ်။

Common mistake

updateDoc က document မရှိရင် error ဖြစ်နိုင်ပါတယ်။ မရှိရင်အသစ်တည်ဆောက်ချင်တာပါဆိုရင် setDoc နှင့် { merge: true } ကိုစဉ်းစားပါ။

Data ပြင်ဆင်ခြင်း (Update) | Thuta Learning