Search code examples
javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functions

Use firebase-functions for firebase v2 cloud functions


How can I write these functions such that there work with v2.

const {onSchedule} = require("firebase-functions/v2/scheduler");
const {logger} = require("firebase-functions");
const {functions} = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const {initializeApp} = require("firebase-admin/app");
const firebaseTools = require("firebase-tools");

initializeApp();

async function deletePath(path) {
  await firebaseTools.firestore
      .delete(path, {
        project: process.env.GCLOUD_PROJECT,
        recursive: true,
        force: true,
        token: functions.config().fb.token,
      });
}

The cloud console is giving me this error:

Error: functions.config() is no longer available in Cloud Functions for Firebase v2. Please see the latest documentation for information on how to transition to using environment variables

I have checked the documentation. There are some examples with onDocumentCreated. I could figure out how that could help me.


Solution

  • As mentioned by @Doug Stevenson:

    You're supposed to replace the use of the old functions.config() with environment variables from the documentation you read. Use environment variables to access configuration values

    const fbToken = process.env.FB_TOKEN; // Configure this as a variable in the environment
    
    async function deletePath(path) {
      await firebaseTools.firestore
        .delete(path, {
          project: process.env.GCLOUD_PROJECT,
          recursive: true,
          force: true,
          token: fbToken,
        });
    }
    

    Then

    Use firebase deploy --only functions to deploy v2 functions.

    Reference: