// Initialize Cloud Firestore and get a reference to the service
import { getFirestore } from "firebase/firestore";
const db = getFirestore(app);
Collection create automatically when you add a document to it
import { collection, addDoc } from "firebase/firestore";
try {
await addDoc(collection("DB OBJECT", "COLLECTION NAME"), {DOCUMENT});
} catch (e) {
console.error("Error adding document: ", e);
}
EXAMPLE
import { collection, addDoc } from "firebase/firestore";
try {
const docRef = await addDoc(collection(db, "users"), {
first: "Ada",
last: "Lovelace",
born: 1815
});
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
NOTE : Documents in a collection can contain different sets of information.
Retrive all collection documents
getDocs(collection(db , 'collection name'))
Example
import { collection, getDocs } from "firebase/firestore";
const res = await getDocs(collection(db, "users"));
res.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
});
const q = query(collection(db, "cities"), where("capital", "==", true));
// where(field , condition , condition value)
1.useCollection(firestore.Query)
Login pageSignup pageHome pageList frindesAdd friend + invite friendJoin puplic gameCreate new gameGame roomGame show ruleGame room silent statusGame room discussionGame room voteGame room vote resultVictoryGame points + repeat game or back homeRank page
Nov 30, 2023grades
Sep 2, 2023index -> charts and statistics
Jul 24, 2023CORE MODULES GLOBALS global classes example (Math class) cont val = Math.abs(-5); global functions example (fetch) fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => console.log(json))
Jun 4, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up