I'm working with Firebase and initializing the APIs and functions to retrieve data I need in firebase.tsx and then importing them in my pages/index.tsx file but can't access exports after Firebase app initilization
services/firebase.tsx:
import firebase, { FirebaseOptions } from 'firebase/app'
import { getAuth } from 'firebase/auth';
import { collection, doc, setDoc, getFirestore, query, where, getDocs, orderBy } from "firebase/firestore";
import { getStorage } from 'firebase/storage';
import { getAnalytics } from "firebase/analytics";
import config from '../config';
import Blog, { blogConverter } from "../models/Blog";
export const yo = 'yo'
if (!firebase.getApps().length) {
firebase.initializeApp(config.firebaseConfig as FirebaseOptions);
}
export const auth = getAuth();
export const firestore = getFirestore();
export const storage = getStorage();
export const analytics = getAnalytics();
/**
* Get all blogs from firebase
* @returns {Promise<Blog[]>} A promise that resolves to an array of Blog objects
*/
export const getBlogs = async (): Promise<Blog[]> => {
const q = query(
collection(firestore, "blogs"),
where("status", "==", "published"),
where("publish_date", "<", new Date().toUTCString()),
orderBy("publish_date", "desc")
).withConverter(blogConverter);
const querySnapshot = await getDocs(q);
if (querySnapshot.empty) return [];
const allBlogs: Blog[] = querySnapshot.docs.map(doc => doc.data());
return allBlogs;
}
pages/index.tsx
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import { GetServerSideProps } from 'next'
import { getBlogs, yo } from '../services/firebase'
import Link from 'next/link'
import Blog from '../models/Blog'
export const getServerSideProps: GetServerSideProps = async (context) => {
console.log(yo)
const blogs = await getBlogs()
return {
props: {
blogs
}
}
}
interface Props {
blogs: Blog[]
}
const Home: NextPage<Props> = ({ blogs }) => {
...
}
When I move export const yo = 'yo'
after line 15, I get a "cannot access 'yo' before initialization but with where it is now I do not.
It seems like initializing Firebase is delaying the initialization of exports to come after it but I have seen other people follow this same pattern?
What am I missing?