Firecloud功能未触发?

时间:2019-10-30 19:15:21

标签: javascript firebase google-cloud-functions

这是我的功能,甚至没有被Firebase功能云触发!我收到一条错误消息:错误:函数执行失败。细节: 无法读取未定义的属性“ ref”

     //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)
          })


    });

1 个答案:

答案 0 :(得分:1)

onCreate更改为以下内容:


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

对于您而言,要获得root,请执行以下操作:

const roots = snap.ref.root

您可以在此处找到更多信息:

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