繁体   English   中英

GeoFire + Firebase 函数 + 函数。https.onCall 不返回数据

[英]GeoFire + Firebase functions + functions.https.onCall not returning data

请解决我的问题@frank

我在这里尝试做的是,从 Android 应用程序调用 Firebase 函数到 geoFire 半径内的 searchText 并尝试返回匹配字符串的键数组,我得到所有 Z78E6221F6393D1356681DB398F 时正确获取日志,但无法返回 6/CED1结果是我的应用程序。 我期待我的应用程序中有任何键数组。 我想在我的应用程序中承诺/返回的东西或投射 Array 确实有问题。 请我弄清楚;)

这是我的 firebase function。

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

exports.geoKeySearchFunction = functions.https.onCall((data, context) => {
  const varLatitude = Number(data.latitude);
  const varLongitude = Number(data.longitude);
  const varRadius = Number(data.radius);
  //it string
  const varCity = String(data.city);
  const varSearch = String(data.searchText);
  //declare array to return
  var arrayToReturn = [];
  //console.log('data.latitude', data.latitude);

  const ref = admin.database().ref().child("geoBiz");
  const varGeoFire = new GeoFire(ref)

  return new Promise(function (resolve, reject) {
    const ref = admin.database().ref().child("geoBiz");
    const geoFire = new GeoFire(ref);

    var geoQuery = varGeoFire.query({
      center: [varLatitude, varLongitude],
      radius: varRadius
    })

    geoQuery.on("key_entered", function (key, location, distance) {
      console.log('key_entered key', key);
      //ref to like  PUNE/bizUID/BN 

      varUID = key;

      return admin.database().ref(varCity + '/' + key + '/BN').once("value")
        .then(snapshot => {
          console.log('snapshot.key', snapshot.key);
          console.log('snapshot.val()', snapshot.val());

          var value = String(snapshot.val());
          //includes return true if matched strin found : javascript syntax
          if (value.includes(varSearch, 0)) {
            //todo; create array of keys whose string matched and return array to android
            console.log('value.includes(varSearch, 0)');
            arrayToReturn.push(varUID);//push javasrcipt syntax like add
            console.log('arrayToReturn loop', arrayToReturn);

          }
          console.log('arrayToReturn out', arrayToReturn);
          //aaray is filled here
          return arrayToReturn;
        }).catch(error => {
          console.error(error);
          res.error(500);
        });
    })
    geoQuery.on("ready", function () {
      resolve();
    });

    //return array, executes first leading empty array
    //console.log('arrayToReturn end',arrayToReturn);
    //return arrayToReturn;
  });

});

这是我的 Android 代码


    private Task<String> taskSearchGeoKeyFunction(String searchText) {
        // Create data to send to function.
        //Map<String, Object> , string is var name , Object is data that can be cast to any  type as per need in function
        Map<String, Object> data = new HashMap<>();
        data.put("searchText", searchText);
        data.put("latitude", varLati);
        data.put("longitude", varLongi);
        data.put("radius", varRadius);
        data.put("city", varCity);
        data.put("push", true);//

        mListSearchedKeys.clear(); //clear privous results
        return mFunctions
                .getHttpsCallable("geoKeySearchFunction")//name of function
                .call(data)
                .continueWith(new Continuation<HttpsCallableResult, String>() {
                    @Override
                    public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                        // This continuation runs on either success or failure, but if the task
                        // has failed then getResult() will throw an Exception which will be
                        // propagated down.
                        //String result = (String) task.getResult().getData();
                        System.out.println("geoKeySearchFunction() task.getResult().getData()____" + task.getResult().getData());
                        System.out.println("geoKeySearchFunction() task.getResult()____" + task.getResult());
                        //return result;

                        //+10 result.getData() gets datatype wwhich is retuurned from function; so to use cast the type like array or map and use data
                        HttpsCallableResult result = task.getResult();
                        //mListSearchedKeys=(List<String>) result.getData();
                        if (result.getData() != null) {
                            System.out.println("geoKeySearchFunction() result.getData()____" +result.getData());

                            //return result.getData().toString();
                        }

                        return null;

                    }
                } ).addOnCompleteListener( new OnCompleteListener<String>() {
                    @Override
                    public void onComplete(@NonNull Task<String> task) {
                        String result = task.getResult();
                        System.out.println("geoKeySearchFunction "+task.getResult() +" SUCCESS"+task.isSuccessful());
                    }
                } );

    }

``````      
also
as i am trying to return array from function , how do i properly cast  HttpsCallableResult to get Array, or should I use Object, if so how? 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM