0
votes

I am facing an issue with my firestore cloud functions. I am trying to set up a trigger to send a notification when a new document is added.

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

/*exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello ninjas!");
});*/

// Function to be called when new event occurs
const createNotification = notification => {
  return admin
    .firestore()
    .collection("notifications")
    .add(notification)
    .then(doc => console.log("Notification added", doc));
};

//Trigger when new project is created
exports.projectCreated = functions.firestore
  .document("project/{projectId}")
  .onCreate(doc => {
    const project = doc.data();
    const notification = {
      content: "Added a new project",
      time: admin.firestore.FieldValue.serverTimestamp()
    };

    return createNotification(notification);
  });

On the client side when I add a new project I am able to see the console message that notification is added but when I check the logs in the Cloud function I cannot see any logs. Am I missing anything here?

1
Have you tried reloading the console? - Doug Stevenson
@DougStevenson Considering it takes a few minutes for the new changes to take effect, yes, I did. - Vishesh Thakur

1 Answers

0
votes

Replace

.onCreate(doc => {

with

.onCreate((snap, context) => {

as described at

https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore