I am just creating a onCreate trigger in firebase and it is not working for me.
const admin = require("firebase-admin");
const functions = require('firebase-functions');
var serviceAccount = require("./abc.json");
admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://PROJECT_ID.firebaseio.com" });
exports.sendNotifications = functions.firestore .document('messages/{messageId}') .onCreate((snapshot, context) => { functions.logger.debug('This is a test'); })
and this function is successfully deployed and worked when i test it but not trigger automatically when new messageId is created.
I also try many other triggers like onWrite, onDelete etc but no one automatically invoke when data change.
please help and thanks in advance.
I want that it trigger automatically when new chat message come so that i can send chat notification to second person.
Do you see any error trace within the Cloud Function logs when deploying the function or having the onCreate trigger run on a document creation?
As a follow-up, I could not reproduce this issue after deploying similar code to a new Firebase Cloud Function with an onCreate trigger. Are you using a custom service account to initialize the Firebase Admin SDK? For services deployed to Google Cloud, you can omit in most scenarios using a Service Account key file and initialize with application default credentials.
This is my sample function that you could try:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const firebaseApp = admin.initializeApp();
exports.sendNotifications = functions
.firestore.document("messages/{messageId}").onCreate((snap, ctx) => {
firebaseApp;
functions.logger.debug("This is a test");
});