Home > Enterprise >  Using Firebase Admin in MongoDB Functions
Using Firebase Admin in MongoDB Functions

Time:07-25

I am using MongoDB as my backend and are trying to send notifications to my devices using Firebase and MongoDB App Services. For that I have a trigger with the following function configured:

exports = async function(changeEvent)  {
  const admin = require('firebase-admin');
  const firebaseConfig = {
          apiKey: secrets.apiKey,
          authDomain: secrets.domain,
          projectId: secrets.projectId,
          storageBucket: secrets.bucket,
          messagingSenderId: secrets.senderId,
          appId: secrets.appId,
          measurementId: secrets.measurementId
        };
  admin.initializeApp(firebaseConfig);
  ...
}

However,I get the following error:

"FunctionError: FunctionError: Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error: Error fetching access token: Error: Error while making request: lookup metadata.google.internal on 127.0.0.1:53: no such host metadata.google.internal:80. Error code: undefined"."

Anyone knows what the issue is here?

CodePudding user response:

You're trying to initialize the Admin SDK with the configuration for the client-side SDK. That won't work, as the Admin SDK requires credentials that give it full, administrative access to the project.

You should either initialize the Admin SDK with those credentials as shown in the documentation on initializing the Admin SDK, or you can use the SDK meant for client-side Node usage, which can be initialized with the configuration data you have now.

  • Related