Slown1970
Slown1970

Reputation: 135

Firecloud function not triggered?

This is my function , it's not even triggered by firebase functions cloud ! i get an error message : Error: function execution failed. Details: Cannot read property 'ref' of undefined

     //send the push notification 
    exports.sendPushNotification = functions.database.ref('contacts/').onCreate(event => {

      const root = event.after.ref.root
      var messages = []

      //return the main promise 
      return root.child('/users').once('value').then(function (snapshot) {
          snapshot.forEach(function (childSnapshot) {

              var expoToken = childSnapshot.val().expoToken;

              messages.push({
                  "to": expoToken,
                  "sound": "default",
                  "body": "New Note Added"
              });
          });
          //firebase.database then() respved a single promise that resolves
          //once all the messages have been resolved 
          return Promise.all(messages)

      })
          .then(messages => {
              // console.log(messages)
              fetch('https://exp.host/--/api/v2/push/send', {
                  method: 'POST',
                  headers: {
                      'Accept': 'application/json',
                      'Content-Type': 'application/json',
                  },
                  body: JSON.stringify(messages)

              });
          })
          .catch(reason => {
              console.log(reason)
          })


    });

Upvotes: 0

Views: 70

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

onCreate was changed to the following:


exports.sendPushNotification = functions.database.ref("contacts/").onCreate((snap, context) => {
  const createdData = snap.val();

In your case, to get the root do the following:

const roots = snap.ref.root

You can find more information here:

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

Upvotes: 1

Related Questions