How to get data to page using a module
Unanswered
West African Lion posted this in #help-forum
West African LionOP
I've found a starter template on GitHub which has a module that I need for my app. the module seems to retrieve documents from firebase firestore but I'm not sure how to implement it on my page.
This is the module
This is the module
import firebase_app from "./firebase";
import { getFirestore, doc, getDoc, DocumentData, DocumentSnapshot } from "firebase/firestore";
// Get the Firestore instance
const db = getFirestore(firebase_app);
// Function to retrieve a document from a Firestore collection
export default async function getDocument(collection: string, id: string): Promise<{ result: DocumentSnapshot<DocumentData> | null, error: Error | null }> {
// Create a document reference using the provided collection and ID
const docRef = doc(db, collection, id);
// Variable to store the result of the operation
let result: DocumentSnapshot<DocumentData> | null = null;
// Variable to store any error that occurs during the operation
let error: Error | null = null;
try {
// Retrieve the document using the document reference
result = await getDoc(docRef);
} catch (e) {
// Catch and store any error that occurs during the operation
error = e as Error;
}
// Return the result and error as an object
return { result, error };
}2 Replies
West African LionOP
This would be the page that I want to use the module to retrieve the data according to the sign in user. Used AuthContext to have user stay sign in even after refreshing the browser.
'use client'
import { useAuthContext } from "@/context/AuthContext";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
function Page(): JSX.Element {
// Access the user object from the authentication context
// const { user } = useAuthContext();
const { user } = useAuthContext() as { user: any }; // Use 'as' to assert the type as { user: any }
const router = useRouter();
useEffect( () => {
// Redirect to the home page if the user is not logged in
if ( user == null ) {
router.push( "/" );
}
// }, [ user ] );
}, [ user, router ] ); // Include 'router' in the dependency array to resolve eslint warningThis is what i was hoping to get using the module from the database