Thuta Learning
Firebase Tutorial
IntermediateDevOps & Toolsအလယ်အလတ်

Data ထည့်ခြင်း (Add)

Firebase Tutorialသင်ခန်းစာ 14

Data ထည့်ခြင်း (Add)

addDoc က document ID ကို Firebase က auto-generate လုပ်ပေးပါတယ်။ Notes, posts, orders, messages လို item အသစ်တွေ တစ်ခုချင်းထည့်တဲ့အခါ အဆင်ပြေပါတယ်။

Code Example

javascript
import { getFirestore, collection, addDoc, serverTimestamp } from 'firebase/firestore';
import { getAuth } from 'firebase/auth';

const db = getFirestore();
const auth = getAuth();

async function createNote(title, content) {
  const user = auth.currentUser;
  if (!user) throw new Error('Login required');

  const docRef = await addDoc(collection(db, 'notes'), {
    ownerId: user.uid,
    title: title,
    content: content,
    createdAt: serverTimestamp(),
    isPinned: false
  });

  console.log('New note ID:', docRef.id);
}

ဘာကိုသတိထားရမလဲ?

serverTimestamp() သုံးတာက user device time ကိုမယုံဘဲ server time နဲ့သိမ်းနိုင်လို့ ပိုမှန်ပါတယ်။ Order, post, chat message လို created time အရေးကြီးတဲ့ data တွေအတွက် အသုံးဝင်ပါတယ်။

Expected outputNew note ID: Lk9aPq82xYzExample

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Data ထည့်ခြင်း (Add) | Thuta Learning