云函数如何实现等待while完成再返回?
以下代码实现是聚合统计,由于数量比较多,所以通过while循环(返回数量==100时继续),设置limit分次统计(不想设置太大,不知道官方什么时候会限制)。
但不知道如何实现等待while,请指教,谢谢
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const $ = db.command.aggregate
exports.main = async (event, context) => {
const THIS_COLLECTION = 'workOrder'
const MAX_LIMIT = 100
var thisCount = 100
var i = 0
var thisTotal = []
while (thisCount == 100) {
const thisResult = db.collection(THIS_COLLECTION).aggregate()
.match({
isTask: true
})
.group({
_id: {
responsibleRegion: '$responsibleRegion',
responsibleGrid: '$responsibleGrid',
responsibleName: '$responsibleName',
workType: '$workType',
isComplete: '$isComplete'
},
count: $.sum(1)
})
.skip(i * MAX_LIMIT)
.limit(MAX_LIMIT)
.end()
thisTotal.push(thisResult.list)
thisCount = 99
i++
}
// 等待while后返回
return thisTotal
}
2 回复
如果非要分批,请参考楼下的。我是建议解决突破limit限制。
https://developers.weixin.qq.com/community/develop/article/doc/0008ea04120a18f6988b86d065b013