如何将数组从一个函数传递到另一个函数作为 Cloud Function 中的引用

How to pass an array from one function to another as reference in Cloud Function

我有一个函数 getUsers,其中我有一个数组 jsonResponse。我将该数组传递给 computeData 函数。我希望 computeData 函数应该能够在 jsonResponse 数组本身中添加项目。

但是下面的代码没有添加到同一个数组中,因为它总是 return response.send 函数中的空数组。

index.js

exports.getUsers = functions.https.onRequest((request, response) => {
  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 < 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);
}

有谁知道如何在 Cloud Function for Firebase 中将数组作为参考传递并向其中添加项目?

您的 else 语句是一个 promise,这意味着您的循环将在您的 else 语句中到达 computeData() 时完成并调用 response.send(jsonResponse);

尝试这样的事情,它不会触及所有变量,但主要思想是使用 Promise.all 和计算值作为 resolved -

exports.getUsers = functions.https.onRequest((request, response) => {
  // blah blah
  var y = []; // store promises that resolves your computed value
  snapshot.forEach(function(item) {
    // blah blah
    if (xLocation == null) {
      y.push(Promise.resolve(computeData());
    } else {
      y.push(computeAnotherData(userId));
    }
  });

  Promise.all(y)
    .then(values => {
      response.send(values);
    });
});

function computeAnotherData(userId) {
  let geoFire = new GeoFire(db.ref("/users/" + userId));
  return geoFire.get("location").then(function(location) {
    return computeData();
  });
}

希望它有意义。