Firebase Cloud功能仅在某些时候执行

时间:2019-02-25 06:25:32

标签: node.js firebase firebase-cloud-messaging google-cloud-functions

我编写了一个简单的Firebase Cloud函数,以使用FCM发送通知,我想检查由OnCreate触发的节点是否有一个名为data的子节点,然后发送包含数据的通知,但如果没有,则发送正常的通知,但是当我登录时它给了我

  

Function returned undefined, expected Promise or value

我认为它是基于我的if语句的,可以得到任何帮助,我对NodeJ还是陌生的,谢谢。

相关代码的一个块是:

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



var FCM = require('fcm-node');
var serverKey = 'KEY';
var fcm = new FCM(serverKey);


exports.SendNotification = functions.database.ref('Notification/{pushKey}/{timeStamp}').onCreate((snapshot, context) =>{


    const pushKey = context.params.pushKey;
    const timeStamp = context.params.timeStamp;



    var mRef = admin.database().ref(`Notification/${pushKey}/${timeStamp}`);

    mRef.on('value', function(snapshot){

        console.log('the Reference is ', mRef);
        console.log('snapshot is ', snapshot.val());

    var mToken = snapshot.child('token').val();
    var mTitle = snapshot.child('title').val();
    var mBody = snapshot.child('body').val();


    console.log('The value of the token is ', mToken);

    console.log('The value of the title is ', mTitle);

    console.log('The value of the body is ', mBody);


    if(snapshot.child('data').exists()){
        var one = snapshot.child('data').child('one').val();
        var two= snapshot.child('data').child('two').val();
        var three= snapshot.child('data').child('three').val();





var messageWData = {

    to: mToken,
        notification:{
        title: mTitle,
        body: mBody
    },
        data:{
            one: one,
            two: two,
            three: three
    }
 };
 fcm.send(messageWData, function(err, response){

    if(err){
        console.log('something went wrong!');
    }
    else{
        console.log('successfully sent with response: ', response);
    }
});
    }
    else{
        var message = {

            to: mToken,
            notification:{
                title: mTitle,
                body: mBody
            }
        };


fcm.send(message, function(err, response){

    if(err){
        console.log('something went wrong!');
    }

    else{
        console.log('successfully sent with response: ', response);
    }
});
    }}, function(errorObject){
        console.log('read failed: '+ errorObject.code);
    });
});

0 个答案:

没有答案