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

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

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

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

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