nuxt-community / firebase-module

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

Use in generate in nuxt.config.js #554

Open simeon9696 opened 3 years ago

simeon9696 commented 3 years ago

Not a bug just a general question. How would we access firestorein the generate hook?

simeon9696 commented 3 years ago

I'm using this atm but I'm not sure if this is the right way to do it

  generate:{

    async routes(){
      try {

        const { default:firebase } = await import('firebase/app');

        await import('firebase/firestore');
        const config ={
          apiKey: process.env.apiKey,
          authDomain: process.env.authDomain,
          databaseURL: process.env.databaseURL,
          projectId: process.env.projectId,
          storageBucket: process.env.storageBucket,
          messagingSenderId: process.env.messagingSenderId,
          appId: process.env.appId,
          measurementId: process.env.measurementId
        }
        if (!firebase.apps.length) {
          firebase.initializeApp(config);
        }

        const app = firebase.apps[0];

        const l = await app.firestore().collection('products').get();
        const products = l.docs.map(doc=> { return {payload: {...doc.data(), id: doc.id}, route: `/product/${doc.data().category}`}})
        return products;
      } catch (error) {
        console.error(error)
      }
    }
Dwiga commented 2 years ago

Hi, I got an issue with this line firebase.apps.length it says Cannot read property 'apps' of undefined did you know something?

luckstar77 commented 2 years ago

Hi, I got an issue with this line firebase.apps.length it says Cannot read property 'apps' of undefined did you know something?

I also have this problem.

vacharanon commented 2 years ago

@Dwiga @luckstar77 Check your firebase version. Make sure you use this module with firebase SDK <= 8

luckstar77 commented 2 years ago

Thank you, I downgrade to v7.24.0, it is solved.

Dwiga commented 2 years ago

@requiem202 hi, thank you it works after I downgrade my firebase SDK to 7.24.0

simeon9696 commented 2 years ago

@luckstar77 @Dwiga you can try firebase v8.10.0 but definitely not v9+