nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
641 stars 99 forks source link

Firestore - enablePersistence() throws "Firestore has already been started and its settings can no longer be changed." when used with emulators #451

Closed GuasaPlay closed 3 years ago

GuasaPlay commented 3 years ago

Hello everyone.

image

Firestore has already been started and its settings can no longer be changed. You can only modify settings before calling any other methods on a Firestore object.

I have a problem when I try to use the emulator in firestore. In previous issues #390 #116 they have the same problem, I followed the same steps to correct the problem but it still shows me the error. Maybe you can help me with something that I am doing wrong This is my config in nuxt.config.js

image image

miketierce commented 3 years ago

I was facing the same issue. This is my workaround.

basically, I uncomment the emulator lines in development and comment them back out before deploy

// snippet from nuxt-firebase settings found in nuxt-config.js
      firestore: {
        enablePersistence: true
        // emulatorPort: process.env.NODE_ENV !== 'production' ? 8080 : undefined,
        // emulatorHost: 'localhost'
      }
// nuxt-config.js
  render: {
    bundleRenderer: {
      runInNewContext: false
    }
  },  
  ssr: true,
  firebase: {
    config: {
      // config
    },
    services: {
      auth: {
        ssr: true,
        persistence: 'local',
        initialize: {
          onAuthStateChangedMutation: null,
          onAuthStateChangedAction: 'user/SET_AUTH_USER'
        }
      },
      firestore: {
        enablePersistence: true
        // emulatorPort: process.env.NODE_ENV !== 'production' ? 8080 : undefined,
        // emulatorHost: 'localhost'
      }
    }
  },

With this setup, I'm able to bind to docs running in the emulator during development, and bind to the actual firestore in production

lupas commented 3 years ago

@GuasaPlay @miketierce I can't really reproduce this issue, could you create a sample project on https://template.nuxtjs.org/ (Codesandbox) where this issue appears so I can debug accordingly?

Are you sure you have the newest version of Firebase & nuxtjs/firebase installed and don't import Firebase anywhere else in your project?

miketierce commented 3 years ago

Oh my. I think I am importing Firebase into the login component as well.

I'll try cleaning that up first.

GuasaPlay commented 3 years ago

@miketierce could you explain more about this?

tibs245 commented 3 years ago

I have the same problem as @miketierce. When I have "firestore / enablePersistence" option to true (and emulatorHost / emulatorPort configured). I have the error message : Firestore has already been started and its settings can no longer be changed.

For bypass the problem I set firestore => enablePersistence to false in development mode.

I have try to reproduce it on codesandbox as @lupas asked. But I have a others problem when I install firebase dependance on codesandbox : https://codesandbox.io/s/pedantic-shockley-wluw9?file=/package.json (Not relevance)

buwilliams commented 3 years ago

+1 This is still an issue.

miketierce commented 3 years ago

It turns out I had a few other places in my code that I was still importing and using firebase instead of using this.$fire

Removing those additional import statements solved the problem for me.

lupas commented 3 years ago

@tibs245 & @buwilliams are you guys sure you're not importing Firebase somewhere else in your project?

This module imports Firebase for you, you shouldn't be importing Firebase yourself additionally.

buwilliams commented 3 years ago

@lupas As far as I can tell (manually going through all the code as well as grepping) the answer is no. All I need to do is enablePersistence to get this error. It may help to know this is not a SSR project.

firestore: {
    enablePersistence: {
        synchronizeTabs: true
    },
    emulatorPort: process.env.NODE_ENV === 'development' ? 8080 : undefined,
}
tibs245 commented 3 years ago

@lupas Yes I am sure. I have do search on all project file and I not found import.

lupas commented 3 years ago

Hey @tibs245 & @buwilliams

Thanks for your inputs! Found the issue and fixed it with https://github.com/nuxt-community/firebase-module/commit/bc16b45a712e3c6400f7c9f8fa1af4233679b46c, will release in a new version asap.

Issue was that we called useEmulator() before enablePersistence(). Switching that order works perfectly without throwing the error.

lupas commented 3 years ago

Fixed with v7.5.0, thanks everyone!

buwilliams commented 3 years ago

@lupas I'm impressed you took the time to dig in and solve it. I'd say this is a +1 for your character sir. I hope you have an extra nice Friday!