-1

I am getting value inside token variable but the code is producing error saying that function should return promise or value. the reason for which I would like to access token is to send notification to the receiver about the received request. my problematic code snippet is present below.

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

 exports.sendRequestReceivedNotification=functions
        .database
        .ref('/Requests/{receiverID}/receivedFrom/{senderID}')
        .onCreate((snapshot,context)=>{
            const receiverID= context.params.receiverID;
            const senderID=context.params.senderID;
            console.log(senderID,' sent message to ',receiverID);


            admin.database().ref()
                            .child('DeviceTokens')
                            .child(receiverID)
                            .child('deviceToken')
                            .once('value')
                            .then(function(snapshot){
                                var token=snapshot.val();
                                console.log(token);
                                return true;
                            })
                            .catch(function(error){
                                return false;
                            });

            });

while deploying, running and triggering the function i am getting the error as shown in the following snapshot. I have googled abut promises, then().catch() structure but could not get the grasp and the illusion has been never clear to me... :(

return undeifned value:

enter image description here

Could someone be kind enough to explain the issue here and remedy to this problem please?

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Shresh
  • 11
  • 3

1 Answers1

0

The reason that your function returns undefined is that you never return the value from it. To return the result of your complex computation (with all the promises), add return before you start the chain:

return admin.database().ref()
       // all other calls

Of course you can store the result into a variable, and return it later:

const result = admin.database().ref() // and the rest of the chain
// other things you do
return result;

Another way to return a value, which is not very useful in this particular case, but could theoretically save you from having this issue, is using a short syntax of arrow functions, as you use them anyway:

// in case the body of a function can be a single expression,
// it is returned implicitly
(param1, param2, …, paramN) => expression
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
  • And is it like the return value from the above function is going to be either true or false? And it seems like i cant create other codes beneath this function. On trying doing so , I get unreachable code error. does it mean that I have to specify rest of the logic inside then () .. Please dont bother to answer :) – Shresh Jul 09 '18 at 13:14
  • I added another option that would let you to save the result, do other stuff, and then return it. – Yury Fedorov Jul 09 '18 at 14:50