#How to link and get data through module file

1 messages · Page 1 of 1 (latest)

whole lava
#

Hey, sorry this my first time using tsx and javascript. I'm trying to link a module called getData with a page but I'm not sure how to do it. The code below is wrong i believe since I'm trying to migrate the way of linking firebase database using some of my old code.

This is the getData module i'm trying to use:

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 };
}
#

This one is the page which I'm trying to use it and link but i'm not sure how to:

wise sun
#

I don't know what you mean by "link". There also seems to be a lot of unrealated code there.

#

Maybe you should provide an example code that has only what you are trying to do.

whole lava
#

oh well i'm trying to get data from the firestore database such as student and lectures, but when i refresh the page it will log me out.

#

So i found a person that made a starter firebase nextjs template it has the AuthContext which would have user stay signed in even when refresh

wise sun
#

I don't really have specific knowledge on Next or Firebase unfortunately

whole lava
#

he also provided getData as a way to get data from the database so i'm trying to use that with the authcontext and the getdata together

#

ah alright

wise sun
#

This server is mostly used for type specific issues, so getting help with usage of javascript libraries here may be a bit patchy

#

Not that it is impossbible

#

Reactiflux is mentioned, and also the Next discord server. They might be better for this type of question. Maybe there is a firebase discord too?

whole lava
#

i did put my question on it so far haven't got a respond baack yet

#

havent been to reactiflux i guess i'll check that one out

wise sun
#

SpeakJS is a general JS server. It's so-so for questions