Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase functions.https.onCall works but returns null

I have the following firebase cloud function written in node.js that I call from my Android app

exports.findNearestBranch = functions.https.onCall((data, context) => { 
  var latitutde = data.lat;
  var longitude = data.long;

  var ret;

  return getLocationObject(latitutde,longitude)
    .then(function(result){
      var fromObject=result;
      console.log('CONTEXT CLIENT '+latitutde+' LONG '+longitude);

      calculateNearestBranch(fromObject)
        .then(function(result){
          console.log("TO APP "+JSON.stringify(result));
          ret=result;
        })
        .catch(function(error){

        });
    })
    .catch(function(error){

    });
});

The function works fine but I get null when trying to get results in Android with the following method

private Task<String> inputCurrentLocation(String[] geoLocations) { 
  Map<String, Object> data = new HashMap<>();
  data.put( "lat", geoLocations[0] );
  data.put( "long", geoLocations[1] );

  return mFunctions
    .getHttpsCallable( "findNearestBranch" )
    .call( data )
    .continueWith( new Continuation<HttpsCallableResult, String>() {
      @Override
      public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
        String result = (String) task.getResult().getData();
        return result;
      }
    } ).addOnCompleteListener( new OnCompleteListener<String>() {
      @Override
      public void onComplete(@NonNull Task<String> task) {
        String result = task.getResult();
        System.out.println("RESULT FROM NODE "+result+" SUCCESS"+task.isSuccessful());
      }
    } );
}

I have wasted countless hours online trying to find what is wrong with no success. Someone please point where my problem is.

like image 257
MbaiMburu Avatar asked Oct 03 '18 10:10

MbaiMburu


1 Answers

You're not returning anything in your https callable.

Try adding a return to the calculateNearestBranch function and have that function return the result variable.

Also you should put some logging into the catch statements so your callable won't fail silently.

exports.findNearestBranch = functions.https.onCall((data, context) => { 
  var latitutde = data.lat;
  var longitude = data.long;

  return getLocationObject(latitutde,longitude)
    .then(function(result){
      var fromObject=result;
      console.log('CONTEXT CLIENT '+latitutde+' LONG '+longitude);

      // Added `return`
      return calculateNearestBranch(fromObject)
        .then(function(result){
          console.log("TO APP "+JSON.stringify(result));

          // Returned `result`
          return result;
        })
        .catch(function(error){

        });
    })
    .catch(function(error){

    });
});
like image 154
sketchthat Avatar answered Oct 16 '22 10:10

sketchthat