title: How to pass an array from one function to another as reference in Cloud Function
body: <p>I have one function <code>getUsers</code> where I have one array <code>jsonResponse</code>. I am passing that array to <code>computeData</code> function. I want <code>computeData</code> function should be able to add items in <code>jsonResponse</code> array itself.</p>
  
  <p>But below code is not adding in same array, as it always return empty array in <code>response.send</code> function.</p>
  
  <p>index.js</p>
  
  <pre><code>exports.getUsers = functions.https.onRequest((request, response) =&gt; {
    var x = [];
  
    var xLocation,
      yLocation = [],
      jsonResponse = [];
  
    // print algorithm name
    console.log(request.query.algo);
  
    let geoFire = new GeoFire(db.ref("/users/" + request.query.userId));
  
    geoFire.get("location").then(function(location) {
      xLocation = location;
    });
  
    db
      .ref("/users/" + request.query.userId)
      .once("value")
      .then(function(snapshot) {
        var jsonObject = snapshot.val();
        var basicProfileJsonObject = jsonObject.basicProfile;
        for (var key in basicProfileJsonObject) {
          if (utils.isNumber(basicProfileJsonObject[key])) {
            x.push(basicProfileJsonObject[key]);
          }
        }
        db.ref("/users/").once("value").then(function(snapshot) {
          var y = [];
          snapshot.forEach(function(item) {
            var user = item.val();
            let userId = user.basicProfile.userId;
            if (userId !== request.query.userId) {
              if (xLocation == null) {
                computeData(x, user, request.query.algo, jsonResponse);
              } else {
                let geoFire = new GeoFire(db.ref("/users/" + userId));
                geoFire.get("location").then(function(location) {
                  if (location === null) {
                    console.log(
                      "Provided key is not in GeoFire, will ignore profile"
                    );
                    computeData(x, user, request.query.algo, jsonResponse);
                  } else {
                    console.log("Provided key has a location of " + location);
                    var distance = GeoFire.distance(xLocation, location); // in km
                    console.log("Distance: " + distance);
  
                    if (distance &lt; 15) {
                      computeData(x, user, request.query.algo, jsonResponse);
                    }
                  }
                });
              }
            }
          });
          response.send(jsonResponse);
        });
      });
  });
  
  function computeData(x, user, algo, jsonResponse) {
    var similarityCount,
      y = [];
  
    var basicProfileJsonObject = user.basicProfile;
    for (var key in basicProfileJsonObject) {
      if (utils.isNumber(basicProfileJsonObject[key])) {
        y.push(basicProfileJsonObject[key]);
      }
    }
  
    if (algo === "cosine") {
      // compute cosine value
      similarityCount = cosineUtils.cosineSimilarity(x, y);
    } else if (algo == "euclidean") {
      // compute euclidean distance value
      similarityCount = 1 / (1 + euclidean(x, y));
    } else if (algo === "pearson-correlation") {
      // compute pearson correlation coefficents
      similarityCount = pcorr.pearsonCorrelation(x, y);
    }
    console.log(x);
    console.log(y);
    console.log(similarityCount);
    jsonResponse.push(user);
  }
  </code></pre>
  
  <p>Does anyone know how to pass array as reference and add items into it in Cloud Function for Firebase ?</p>