firebase-module icon indicating copy to clipboard operation
firebase-module copied to clipboard

Use in generate in nuxt.config.js

Open simeon9696 opened this issue 3 years ago • 7 comments

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

simeon9696 avatar Jun 20 '21 00:06 simeon9696

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)
      }
    }

simeon9696 avatar Jun 20 '21 12:06 simeon9696

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

Dwiga avatar Sep 09 '21 00:09 Dwiga

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.

luckstar77 avatar Sep 12 '21 00:09 luckstar77

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

vacharanon avatar Sep 12 '21 09:09 vacharanon

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

luckstar77 avatar Sep 12 '21 10:09 luckstar77

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

Dwiga avatar Sep 12 '21 11:09 Dwiga

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

simeon9696 avatar Sep 22 '21 15:09 simeon9696