Skip to content

Instantly share code, notes, and snippets.

@willie-hung
Last active July 13, 2023 16:50
Show Gist options
  • Save willie-hung/e07116206838143ede25e6b5e27355d7 to your computer and use it in GitHub Desktop.
Save willie-hung/e07116206838143ede25e6b5e27355d7 to your computer and use it in GitHub Desktop.
post_7
import { db } from "./firebaseConfig";
import { collection, doc, getDocs, updateDoc } from "firebase/firestore";
async function addData(name: string, age: number, college: string) {
const objectsRef = collection(db, "test");
try {
const querySnapshot = await getDocs(objectsRef);
querySnapshot.forEach(async (docSnapshot) => {
const documentId = docSnapshot.id;
const documentRef = doc(objectsRef, documentId);
try {
// Update the document with the new field
await updateDoc(documentRef, {
name: name,
age: age,
college: college,
});
console.log(
`Field "name", "age", "college" added successfully to document "${documentId}"`
);
} catch (error) {
console.error(
`Error adding field "name", "age", "college" to document "${documentId}":`,
error
);
}
});
} catch (error) {
console.error("Error retrieving documents:", error);
}
}
// Call the function with the desired expected delivery date
addData("Sung-Jie Hung", 24, "University of Chicago");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment