【发布时间】:2020-10-17 22:59:11
【问题描述】:
我有一个类似这样的数据库。我想如何比较所有用户的价值以获得最大价值。
restaurant
-userUid
-stateUid
-restaurantUid
-price = 9
-restaurantUid2
-price = 10
-stateUid2
-restaurantUid3
-price = 2
正如你可以看到那里的数据库,stateUid 价格是 19 而stateUid2 价格只有 2
所以,stateUid 的价格最高。如何比较它们并显示最多。谢谢
编辑:
我做过类似的事情,在return 出现错误。并且该值不起作用。
exports.calculateTotal = functions.database.ref('/restaurant/{userUid}/{stateUid}/{restaurantUid}')
.onWrite((change, context) => {
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
//Get id
const restaurantUid = context.params.restaurantUid;
let totalValue = 0;
change.after.forEach(function (item) {
totalValue += item.child('total').val();
});
console.log(totalValue);
return functions.database.ref('/priceTotal/' + restaurantUid).child('total').set(totalValue);
});
【问题讨论】:
-
您是想在 recyclerview 之类的地方显示您的数据集,还是只想获得最高价格的状态?
-
我只想获得最高价状态@50_Seconds_Of_Coding
-
您的编辑使这个问题完全不同,因为它不再与 Android 有关。这意味着现在两个现有答案对这个问题都没有意义。
-
从您对我的回答的评论看来,您在 Cloud Functions 代码中遇到了错误。请编辑您的问题以包含完整的错误和堆栈跟踪。
-
@FrankvanPuffelen nvm,我已经制作了新节点来单独存储它们。谢谢楼主
标签: android firebase firebase-realtime-database google-cloud-functions