Skip to content

Instantly share code, notes, and snippets.

@willie-hung
Created July 13, 2023 17:03
Show Gist options
  • Save willie-hung/c4fd9ae0a1dec38e9660145209b2d1d9 to your computer and use it in GitHub Desktop.
Save willie-hung/c4fd9ae0a1dec38e9660145209b2d1d9 to your computer and use it in GitHub Desktop.
post_7
import { db } from "./firbaseConfig";
import {
collection,
getDocs,
query,
where,
} from "firebase/firestore";
const queryFirestore = async (name: string) => {
const objectsRef = collection(db, "test");
const q = query(objectsRef, where("name", "==", name));
try {
const querySnapshot = await getDocs(q);
if (!querySnapshot.empty) {
const documentSnapshot = querySnapshot.docs[0];
const data = documentSnapshot.data();
console.log("Object searched successfully", data);
} else {
console.log("No matching document found.");
}
} catch (error) {
console.error("Error searching object: ", error);
}
};
queryFirestore("Sung-Jie Hung");
// Output
// Object searched successfully { name: 'Sung-Jie Hung', age: 24, college: 'University of Chicago' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment