question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NextJS compatibility issues with doc(useFirestore(), 'tryreactfire', 'burrito')

See original GitHub issue

I was attempting to just follow the demo but there seems to be some odd behavior when using reactfire paired with any version of Firebase later than 9.1.3 and any version of NextJS v12. Specifically the line: const burritoRef = doc(useFirestore(), 'tryreactfire', 'burrito'); throws FirebaseError: [code=invalid-argument]: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

The issue goes away when I either

  1. Use next@11
  2. Use firebase@9.1.3 (or older)

I have a feeling like this is probably an issue with firebase/nextjs, not reactfire but I figured this could be a good place to start.

Version info

firebase: “9.5.0” next: “12.0.4” react: “17.0.2” reactfire: “4.2.1”

Test case

_app.jsx

import { getFirestore, doc } from 'firebase/firestore';
import { getAuth } from 'firebase/auth';
import { AuthProvider, FirebaseAppProvider, FirestoreProvider, useFirebaseApp, useFirestore, useFirestoreDocData } from 'reactfire';

const firebaseConfig = {
  // config goes here
};

function TestFirestoreData() {
  const burritoRef = doc(useFirestore(), 'tryreactfire', 'burrito'); // this is where the error is thrown
  const { status, data } = useFirestoreDocData(burritoRef);
  console.log(data)
  return <div/>
}

function FirebaseSDKProviders({ children }) {
  const app = useFirebaseApp()
  const auth = getAuth(app)
  const firestore = getFirestore(app)

  return (
    <AuthProvider sdk={auth}>
      <FirestoreProvider sdk={firestore}>
        <TestFirestoreData/>
        {children}
      </FirestoreProvider>
    </AuthProvider>
  )
}

function MyApp({ Component, pageProps }) {
  return (
    <FirebaseAppProvider firebaseConfig={firebaseConfig}>
      <FirebaseSDKProviders>
        <Component {...pageProps} />
      </FirebaseSDKProviders>
    </FirebaseAppProvider>
  )
}

export default MyApp

Steps to reproduce

Replace sample _app.tsx with the above code in a sample app created from yarn create next-app

Expected behavior

Expected code to run without any errors

Actual behavior

Error thrown FirebaseError: [code=invalid-argument]: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

It seems to have some issue with the server side runtime, since I can comment out the TestFirestoreData component, reload the page, then uncomment the TestFirestoreData component (performing a hot reload but not actually reloading the page) and it will work, but when I reload the page I get the above error again.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

10reactions
Gbuompriscocommented, Dec 1, 2021

Can you try adding

experimental: {
  esmExternals: false
}

to your next.config.js?

I spent hours debugging this. I think the issue is in rxfire somehow picking up the CJS version which ends up with the SDK mismatching

0reactions
CubitSpeedcommented, Jan 17, 2022

Update: this fixed the issue for me: https://github.com/firebase/firebase-js-sdk/issues/5823

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firestore/Firebase not working with Next.js - Stack Overflow
I am trying use firebase in my Next.js app, specifically for the api. It works fine when I build production locally, and on...
Read more >
Reactfire: Hooks, Context Providers, and Components That ...
Access Firebase libraries from any component - Need the Firestore SDK? useFirestore . Remote Config? useRemoteConfig . Safely configure Firebase libraries - ...
Read more >
Next.js and Firebase DB full-stack app tutorial | LogRocket Blog
Learn how to set up Firestore's Cloud Firestore and communicate with the database through a Next.js app with API endpoints.
Read more >
FirebaseExtended Reactfire Statistics & Issues - Codesti
reactfire Recent Issues ; NextJS compatibility issues with doc(useFirestore(), 'tryreactfire', 'burrito'), open, 7, 2021-12-01, 2022-12-24 ; Cannot connect to the ...
Read more >
Reactfire - Hooks, Context Providers, and Components that ...
Issues here are answered by maintainers and other community members on GitHub on a ... user ? firebase.firestore().collection('x').doc(user.uid) : null );.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found