I am trying to trigger cloud functions from firestore events (onWrite) but I don't find the correct way to implement it with the version 9 modular that I am using for this project. The whole documentation is with version 8 (named space).
Here is what I am trying to do (version 8) :
export const documentWriteListener = functions.firestore
.document('collection/{documentUid}')
.onWrite((change, context) => {
if (!change.before.exists) {
// New document Created : add one to count
db.doc(docRef).update({ numberOfDocs: FieldValue.increment(1) });
} else if (change.before.exists && change.after.exists) {
// Updating existing document : Do nothing
} else if (!change.after.exists) {
// Deleting document : subtract one from count
db.doc(docRef).update({ numberOfDocs: FieldValue.increment(-1) });
}
return;
});
Here is my version 9 firebase initialization file :
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth";
import { getFunctions } from "firebase/functions"
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = getAuth(app);
const functions = getFunctions(app);
export { db, auth, functions }
and here is what I've tried for the cloud function (that I put in an independent file actionsCount.js) :
import { db, functions } from '../../firebase/initFirebase';
import { updateDoc, doc } from "firebase/firestore";
import * as functions from 'firebase-functions';
export const documentWriteListeners = functions.firestore
.document('actions/{documentUid}')
.onWrite((change, context) => {
const actionsCounter = doc(db, "actionsCount", "counter")
if (!change.before.exists()) {
// New document Created : add one to count
await updateDoc(actionsCounter, { numberOfDocs: FieldValue.increment(1) });
} else if (change.before.exists() && change.after.exists()) {
// Updating existing document : Do nothing
} else if (!change.after.exists()) {
// Deleting document : subtract one from count
await updateDoc(actionsCounter, { numberOfDocs: FieldValue.increment(-1) });
}
return;
})
When I deploy using firebase deploy --only functions
, I get the error : Cannot understand what targets to deploy/serve. No targets in firebase.json match '--only functions'.
Should I put the function somewhere else ? Function is wrong ?
Thanks a lot for your help !
CodePudding user response:
You need to initialize Firebase Functions with the command firebase init
. When you select functions
and proceed with the setup, it'll create a new functions directory containing an index.js/ts
file by default. That's where you add your Cloud Functions.
The firebase-functions
SDK is not meant to be used on client side.
The directory structure would look like this:
firebase deploy --only functions
will deploy the functions.
CodePudding user response:
Finally fixed this! Half a day haha So I obviously ran the functions firebase init as suggested by Dharmaraj. As per the modular (version 9), it looks like we can do it. So I basically tried to initialize the firebase sdk in the functions/index.js file with the version 8 (named-space) methods and it worked. Here is the code :
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
const fieldValue = admin.firestore.FieldValue;
// Updating the Actions document count
exports.documentWriteListeners = functions.firestore.document('actions/{documentUid}').onWrite((change, context) => {
if (!change.before.exists) {
// New document Created : add one to count
db.doc('actionsCount/counter').update({ numberOfDocs: fieldValue.increment(1) });
} else if (change.before.exists && change.after.exists) {
// Updating existing document : Do nothing
} else if (!change.after.exists) {
// Deleting document : subtract one from count
db.doc('actionsCount/counter').update({ numberOfDocs: fieldValue.increment(-1) });
}
return;
})