flamelink / gatsby-source-flamelink

Gatsby source plugin for Flamelink
MIT License
8 stars 9 forks source link

Unable to access firebase app through my project #34

Closed leevi978 closed 4 years ago

leevi978 commented 4 years ago

Hello, I am using gatsby-source-flamelink as a plugin for my Gatsby project. I have a case where I need to initialize a firebase app in order to fetch some files during build-time (within gatsby-node.js). When I try to build, however, I get the following errors as shown below. As far as I understand it, gatsby-source-flamelink has already initialized a firebase app, which is initialized as the default app. I attempted to give my own firebase app a name, but firebase requires that the default app is initialized first and thus throws an error.

I need to be able to either initialize a new firebase app on my own, or be able to access the already initialized firebase app. Both the gatsby-source-flamelink app and my own custom app has the same credentials. My own app is initialized during my own project's gatsby-node.js script.

The errors I get:

` ERROR #11321 PLUGIN

"gatsby-source-flamelink" threw an error while running the sourceNodes lifecycle:

The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.

74 | const startTime = present(); // Initialize Flamelink app 75 |

76 | const app = api.initApp({ | ^ 77 | firebaseConfig, 78 | environment, 79 | dbType

File: node_modules\gatsby-source-flamelink\gatsby-node.js:76:25

Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.

caweidmann commented 4 years ago

Hi, do you perhaps have a full code example of how you are initialising the source plugin and your own Firebase instance? Also, have you tried passing a second param to your initialisation (as described here https://firebase.google.com/docs/web/setup#multiple-projects)?

leevi978 commented 4 years ago

For the source plugin I use the following confic in gatsby-config.js:

{ resolve: 'gatsby-source-flamelink', options: { firebaseConfig: { projectId: process.env.GATSBY_FLAMELINK_PROJECT_ID, clientEmail: process.env.GATSBY_FLAMELINK_CLIENT_EMAIL, privateKey: process.env.GATSBY_FLAMELINK_PRIVATE_KEY.replace(/\n/g, '\n'), databaseURL: process.env.GATSBY_FLAMELINK_DATABASE_URL, storageBucket: process.env.GATSBY_FLAMELINK_STORAGE_BUCKET, }, dbType: 'cf', environment: 'production', content: true, populate: true, navigation: true, globals: true } },

When I initialize a new firebase app, I do so in a seperate file which I then import to gatsby-node.js

` var admin = require('firebase-admin')

var flamelink = require('flamelink/app'); require('flamelink/content'); require('flamelink/storage'); require('flamelink/settings');

const firebaseApp = admin.initializeApp({' credential: admin.credential.cert("[removed for this comment]"), databaseURL: '[removed for this comment]', storageBucket: '[removed for this comment]' }, 'firebase-helper' )

const storage = admin.storage().bucket()

var firestore = firebaseApp.firestore();

const app = flamelink({ firebaseApp, locale: 'no', dbType: 'cf' })

const enApp = flamelink({ firebaseApp, locale: 'en-US', dbType: 'cf' }) `

caweidmann commented 4 years ago

Ok, thanks. I'm thinking perhaps we can expose an "app name" property which can be set so that the app can be initialised twice without conflict. We'll see if we can get to this early next week. How urgent is this?

leevi978 commented 4 years ago

That's great, I appreciate the quick response! To be honest, this is keeping me from my main work, which has a deadline at the end of july. I have some minor tasks to keep me busy for a couple of days, however.

caweidmann commented 4 years ago

We'll look into it and hopefully have a solution for you or at the very least a workaround.

caweidmann commented 4 years ago

@leevi978 we've added a potential fix to our upcoming release. Would you please install it to see if it resolves your issue? You can get the fix by running the following command:

npm i gatsby-source-flamelink@next
caweidmann commented 4 years ago

Changes are in the latest release.

npm i gatsby-source-flamelink
leevi978 commented 4 years ago

Hello again @caweidmann , sorry for the late response. Could you please provide documentation on how to use the new variable? I noticed you added a new variable called name in the helpers/api file. However, in gatsby-node.js, you don't seem to be using it when initializing.

In other words this is the code: const app = api.initApp({ firebaseConfig, environment, dbType })

While I wonder if it should be something like this? const app = api.initApp({ firebaseConfig, environment, dbType }, name)