gpt4 book ai didi

javascript - 如何将数组从一个函数传递到另一个函数作为云函数中的引用

转载 作者:行者123 更新时间:2023-12-03 03:43:02 25 4
gpt4 key购买 nike

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

但是下面的代码没有添加到同一个数组中,因为它总是在 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,这意味着您的循环将在它完成时完成并调用response.send(jsonResponse);获取 else 语句中的 computeData()

尝试这样的方法,它不会触及所有变量,但主要思想是使用 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();
});
}

希望这是有道理的。

关于javascript - 如何将数组从一个函数传递到另一个函数作为云函数中的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45535544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com