skip to Main Content

I’m testing out my cloud functions on emulator and they are not triggering. These functions are meant to make changes in Firestore when there are changes in storage but they are not. Not even showing on the logs as having been initiated.

Cloud Function

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { firestore } = require("firebase-admin/firestore");

exports.imageTrackingAdd = functions.storage
  .object()
  .onFinalize(async (object) => {
    if (object.contentType.startsWith("image/")) {
      const trackingDocRef = admin
        .firestore()
        .collection("tracking")
        .doc("imageTracking");

      return trackingDocRef.update({
        imagesAll: admin.firestore.FieldValue.increment(1),
      });
    } else {
      return null;
    }
  });

firebase.js

const app =
  hostname === "localhost"
    ? initializeApp({
        apiKey: "demo-key",
        authDomain: "demo-domain",
        projectId: "demo-project",
        storageBucket: "demo-bucket",
        appId: "demo-appId",
        messagingSenderId: "demo-id",
        measurementId: "demo-measurementId",
      })
    : initializeApp({
        apiKey: process.env.REACT_APP_APIKEY,
        authDomain: process.env.REACT_APP_AUTHDOMAIN,
        projectId: process.env.REACT_APP_PROJECTID,
        storageBucket: process.env.REACT_APP_STORAGEBUCKET,
        messagingSenderId: process.env.REACT_APP_MESSAGINGSENDERID,
        appId: process.env.REACT_APP_APPID,
        measurementId: process.env.REACT_APP_MEASUREMENTID,
      });

Even when i specify the bucket, the problem persists, but perhaps the problem is with my code or the firebase.js setup. Any help would be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    I ran an http function to show which was the active bucket and even though my firebase.js file specified demo-bucket, the emulator storage was reading from demo-project.appspot.com. I changed the firebase.js bucket to demo-project.appspot.com and it worked. Storage cloud functions are now triggered.


  2. You can check in the following ways:

    1. Imports and Initialization: Make sure you’re initializing Firebase Admin SDK correctly in your Cloud Function code. Also, ensure that you are importing initializeApp from firebase-admin, not firebase.

    const admin = require("firebase-admin");
    admin.initializeApp();

    2. Cloud Function Definition: Your function definition seems fine, but you could add some logging to see if the function is at least being invoked. Place a simple log statement at the beginning of your function to check this.

    exports.imageTrackingAdd = functions.storage
      .object()
      .onFinalize(async (object) => {
        console.log("Function triggered:", object);
        // Rest of your code...
      });

    3. Check Function Logs: If your function is deployed on Firebase, you can check the logs using the Firebase CLI to see if there are any error messages or logs indicating the function execution. Use the following command to view logs:

    firebase functions:log

    4. Check Dependencies: Ensure that your Cloud Function dependencies are up-to-date. You can do this by checking your package.json and ensuring that you have the latest versions of the Firebase packages.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search