- sort函数在小程序端,和云开发的云函数数据不一致问题?
function findTop(members) { /* sort 在客户端小程序云端,排序数据不一致 */ let top = { realName: '', openid: '', price: 0 } if (members.length > 0) { top = members[0] for (let i = 0; i < members.length; i++) { if (top.price < members[i].price) { top = members[i] } } } return top } function findTop2(members){ let top = { realName: '', openid: '', price: 0 } if (members.length > 0) { members.sort(function (a, b) { return b.price - a.price }) console.log(members) top = members[0] } return top } let members=[{ "avatarUrl": "", "nickName": "", "price": 120, "realName": "黄玉如", "createDate": "2020-12-28 14:52:13", "openid": "oalIN5BbjgUnIIJDGVycjxiY3MKE" }, { "avatarUrl": "", "nickName": "", "price": 90, "realName": "王铮", "createDate": "2020-12-28 14:57:37", "openid": "oalIN5Jy9qMHK-sEVmXc8sfmN5-0" }, { "avatarUrl": "", "nickName": "", "price": 150, "realName": "郑兴响", "createDate": "2020-12-28 14:58:11", "openid": "oalIN5POkZXM6g-aWp4NhZVsXMaM" }, { "avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTINV6SGu6wxibeCjiafk3eIX55BDq8JNibmQtUpVcMtMqKb9aAJBlJ1qGfWibXWg9PTib3JKJgQgghkswQ/132", "nickName": "倚歌", "price": 100, "realName": "郑锦平", "createDate": "2020-12-28 15:24:54", "openid": "oalIN5CAm9mGDWgPkqbqqkNLiSuY" }, { "avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/PbRsCBUfCWbcBYorXZJunpBSBtXNHPVfk65A4lyXzEJuVqNibib1P0033CBmkdC7viboVt7EgqdQDK8qicD7PPQfibg/132", "nickName": "林英枝", "price": 100, "realName": "林英枝", "createDate": "2020-12-28 15:26:33", "openid": "oalIN5LIatxlx75Q2kz6Ge5UTnvs" }, { "avatarUrl": "", "nickName": "", "price": 160, "realName": "唐晓秋", "createDate": "2020-12-28 15:38:26", "openid": "oalIN5K1rd-Vs3rYHZk5sxaexu0g" }, { "avatarUrl": "", "nickName": "", "price": 120, "realName": "蔡尚芳", "createDate": "2020-12-28 15:41:32", "openid": "oalIN5LNykPxzyUv5T0Jr0K2u7Y0" }, { "avatarUrl": "", "nickName": "", "price": 120, "realName": "黄文娟", "createDate": "2020-12-28 15:58:13", "openid": "oalIN5BkNvpJEDqszA0OhmnFr6Yc" }, { "avatarUrl": "", "nickName": "", "price": 156, "realName": "郑升", "createDate": "2020-12-28 16:42:13", "openid": "oalIN5HRmVk_QvAixiPJhbi-noj4" }, { "avatarUrl": "", "nickName": "", "price": 98, "realName": "王晓霞", "createDate": "2020-12-28 16:50:32", "openid": "oalIN5LusQTo8Z-B5nnhmL5slP2w" }, { "avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/F5QaS9RCtlywN2gY9ZtJLcNeuzbZLM50s8jaqVfa3XN0gc0059iccuwODWqica90PicKsnfIFibU8G4OX6hibMhPnKA/132", "nickName": "林乐平", "price": 120, "realName": "林乐平", "createDate": "2020-12-28 16:51:42", "openid": "oalIN5BQ7b_FUj9basCugBBqHfpA" }, { "avatarUrl": "", "nickName": "", "price": 120, "realName": "叶雪琴", "createDate": "2020-12-28 17:06:08", "openid": "oalIN5P_MRv8YWlyU3yr_EQEb8Tw" }, { "avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLcf7o6fEe1g3Hy21PpYyKF3u1KsKTOaC3Yd6tibNhopicNMOxVmQ5Mw7cMP5AM1icyIYwvyiaVVBibVyQ/132", "nickName": "王小二", "price": 120, "realName": "王小喆", "createDate": "2020-12-28 17:08:49", "openid": "oalIN5PGcUPWLRpFhfglMiZnhvh4" }, { "avatarUrl": "", "nickName": "", "price": 120, "realName": "龚美兰", "createDate": "2020-12-28 17:10:18", "openid": "oalIN5IppKUaUDhXZDyCTmlwEtEM" }, { "avatarUrl": "", "nickName": "", "price": 160, "realName": "张巧玲", "createDate": "2020-12-28 17:22:44", "openid": "oalIN5NQPAuQFUirAkOfIC1zkSds" }] // findTop2 云开发端返回: 唐晓秋 //findTop 小程序端: 张巧玲 //z这两者都是160的price //q求解
2020-12-28 - 云开发的http SDK 的 aggregate() 有bug,获取的不是sum的值,而是每一条数据
const cloud = require("@cloudbase/node-sdk"); const axios = require('axios') const APP = '$$$小程序' function save_token(app, access_token) { //保存token app.database().collection("token").where({ app: APP }).update({ access_token: access_token, validity_time: new Date(new Date().getTime() + 7100 * 1000) }) .then(res => { console.log(res) }) .catch(err => { console.log(err) }) } exports.main = async (event, context) => { console.log("event", event) const app = cloud.init({ env: cloud.SYMBOL_CURRENT_ENV, }); //获取配置 let config = await app.database().collection("token").where({ app: APP }).get().then(res => { return res.data[0] }) let access_token = config.access_token //获取token if (new Date(config.validity_time).getTime() < new Date().getTime()) { //过期的access_token,获取token let url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + config.appid + "&secret=" + config.secret await axios.get(url).then(res => { save_token(app, res.data.access_token) access_token = res.data.access_token }) .catch(err => { console.log(err) }) } //更新数据 let url = 'https://api.weixin.qq.com/tcb/databasequery?access_token=' + access_token let data = { "env": config.env, "query": `db.collection("money").aggregate().group({_id:null,total:db.command.aggregate.sum("$realCount")}).end()` } console.log(data) let result = {} await axios.post(url, data).then(res => { console.log(res) result = { message: "ok", code: "res" } }).catch(err => { console.log("err", err) result = { message: "fail", code: -1 } }) return result };
2020-11-03