0

I read all the answers I found about this but I still have some problems. Probably something with .ref(), but I can't see what I'm doing wrong. My cloud function is not triggered at all.

DB example: business/{businessId}/reservations/{reservationId}

I want to trigger this function every time when a new reservation is created [a new document is created in reservation collection] (business/{businessId}/reservations/). And then I want to sent a notification message, but that is another thing.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

    exports.sendAdminNotification = functions.database
      .ref("business/{businessId}/reservations")
      .onWrite((event: any) => {
       // It never comes here...
        console.log('Here');

        const payload = {
          notification: {
            title: 'New registration',
            body: 'You have new registration!',
          },
        };

        // You can ignore this part
        admin
        .messaging()
        .sendToDevice('SomeToken', payload)
        .catch(function (error: any) {
          console.log('Notification sent failed:', error);
        });

      });
2
  • While that trigger doesn't do exactly what you described, I would still expect it to trigger every time a new node is created under reservations. Maybe you would like to edit the question to explain exactly what you're doing that should trigger this? Commented Apr 30, 2020 at 21:27
  • @DougStevenson Thank you for the answer, I just updated the description. I hope now it's more clear what I want. Commented Apr 30, 2020 at 21:41

1 Answer 1

2

The fact that you're using the terms "document" and "collection" suggests that your database is Firestore. But what you've written here is a Realtime Database trigger. Realtime Database is a completely different database. Instead, you will need to write a Firestore trigger instead. They begin with functions.firestore.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.