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
I ran an http function to show which was the active bucket and even though my
firebase.js
file specifieddemo-bucket
, the emulator storage was reading fromdemo-project.appspot.com
. I changed thefirebase.js
bucket todemo-project.appspot.com
and it worked. Storage cloud functions are now triggered.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.
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.
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:
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.