Firebase云函数TypeError:无法读取未定义的属性“ ref”

时间:2018-11-10 18:27:30

标签: javascript node.js firebase react-native google-cloud-functions

我使用firebase实时数据库和firebase云,我尝试了以下用户pushid是否具有expoToken的情况,我编写了pushnotification / {id}消息,并使用expoToken发送了消息,(我用expo编写react native,然后尝试发送推送通知)这就是我的用例,只是我的问题是我运行此函数并给我TypeError:无法读取未定义错误的属性“ ref”,我该如何解决

const functions = require('firebase-functions');
var fetch = require('node-fetch');

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

exports.sendPushNotification = functions.database
  .ref('pushnotification/{id}')
  .onCreate(event => {
    const root = event.data.ref.root;

    var messages = [];
    return root
      .child('/users')
      .once('value')
      .then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
          var expoToken = childSnapshot.val().expoToken;
          if (expoToken) {
            messages.push({
              to: expoToken,
              body: 'New node Added'
            });
          }
        });
        return Promise.all(messages);
      })
      .then(() => {
        fetch('https://exp.host/--/api/v2/push/send', {
          method: 'POST',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json'
          },
          body: JSON.stringfy(messages)
        });
      });
  });

0 个答案:

没有答案