Функция Firebase Cloud выполняется только иногда - PullRequest
0 голосов
/ 25 февраля 2019

Я написал простую облачную функцию Firebase для отправки уведомлений с помощью FCM. Я хочу проверить, есть ли у узла, который запускается OnCreate, дочерний элемент с именем data, затем отправить уведомление с данными, но если он не отправит нормальное уведомлениеОднако, когда я регистрируюсь, это дает мне

Function returned undefined, expected Promise or value

Я думаю, что это основано на моем утверждении if, любая помощь может быть оценена, я очень плохо знаком сNodeJs, спасибо.

Блок соответствующего кода:

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);
    });
});
...