Last active
July 13, 2023 16:50
-
-
Save willie-hung/e07116206838143ede25e6b5e27355d7 to your computer and use it in GitHub Desktop.
post_7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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