Firestore works once and then it keeps throwing this error

Viewed 79

I am trying to use this to get a snapshot of the firestore in a Nextjs project. But for some reason it works the first time but as soon as I refresh the page I get the following error and then I have to restart the server.

[FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore] {
  code: 'invalid-argument',
  customData: undefined,
  toString: [Function (anonymous)]
}
import db from "../../firebase";
import { collection, getDocs, orderBy, query } from "firebase/firestore";

export async function getServerSideProps(context) {
  const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

  const session = await getSession(context);
  
  //query from firestore
  const colRef = collection(db, `users/${session.user.email}/orders`);
  const q = query(colRef, orderBy("timestamp", "desc"));
  const snapshot = await getDocs(q);

The db instance is imported from the firebase config file here.

import { getApp, initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
  apiKey: "some-apikey",
  authDomain: "some-authDomain",
  projectId: "some-projectId",
  storageBucket: "some-storageBucket",
  messagingSenderId: "some-messagingSenderId",
  appId: "some-appId",
  measurementId: "some-measurementId",
};

function createFirebaseApp(config) {
  try {
    return getApp();
  } catch {
    return initializeApp(config);
  }
}

const app = createFirebaseApp(firebaseConfig);
const db = getFirestore(app);

export default db;

The query returns the correct data the first time but the second time the error appears. I have also tired with firebase/firestore/lite but the same error appears

0 Answers
Related