0
votes

I need to trigger my functions on two different collections. How to do this?

I have something like this but second function overwrites first and first doesn't work. How to do this?

'use strict'; const functions = require('firebase-functions');const admin = require('firebase-admin');admin.initializeApp(functions.config().firebase); const database = admin.database();

exports.apptTrigger = functions.firestore
    .document('notification/{anydocument}' )
    .onCreate((snap, context) => {

    const title = snap.data().title;
    const messageis = snap.data().content;
    const postId = snap.data().idPost;

    const payLoad = {
        notification:{
            title: title,
            body: messageis,
            sound: "default"
        }
    };

        return admin.messaging().sendToTopic("notification", payLoad);

});

exports.apptTrigger = functions.firestore
    .document('notificationP/{anydocument}')
    .onCreate((snap, context) => {

    const title = snap.data().title;
    const messageis = snap.data().content;
    const postId = snap.data().idPost;

    const payLoad = {
        notification:{
            title: title,
            body: messageis,
            sound: "default"
        }
    };

        return admin.messaging().sendToTopic("notification", payLoad);

});```
1

1 Answers

2
votes

The name of each function must be unique. Right now, you're giving them both the same name "apptTrigger". Try giving them different names.

exports.apptTriggerNotification = functions.firestore...
exports.apptTriggerNotificationP = functions.firestore...