- 当前 Bug 的表现(可附上截图)
- 预期表现
- 复现路径
- 提供一个最简复现 Demo
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const MAX_LIMIT = 100
exports.main = async(event, context) => {
// 先取出集合记录总数
const wxContext = cloud.getWXContext()
const countResult = await db.collection('ORDER').count()
const total = countResult.total
// 计算需分几次取
const batchTimes = Math.ceil(total / 100)
// 承载所有读操作的 promise 的数组
const tasks = [];
const allOrder = [];
const waitOrder = [];
const finishOrder = [];
const cancelOrder = [];
const allOrderNum = total;
const watiOrderNum = 0;
const finishOrderNum = 0;
const cancelOrderNum = 0;
for (let i = 0; i < batchTimes; i++) {
const promise = db.collection('ORDER').where({
USER_OPEN_ID: wxContext.OPENID
}).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
tasks.push(promise)
}
// 等待所有
return (await Promise.all(tasks)).reduce((acc, cur) => {
allOrder = acc.data.concat(cur.data)
console.log(allOrder)
for (let i = allOrder.length - 1; i >= 0; i--) {
if ('W' == allOrder[i].ORDER_STATUS) {
waitOrder.push(allOrder[i])
watiOrderNum = ~~watiOrderNum + 1
console.log(waitOrder)
} else if ('F' == allOrder[i].ORDER_STATUS) {
finishOrder.push(allOrder[i])
finishOrderNum = ~~finishOrderNum + 1
console.log(finishOrder)
} else if ('C' == allOrder[i].ORDER_STATUS) {
cancelOrder.push(allOrder[i])
cancelOrderNum = ~~cancelOrderNum + 1
console.log(cancelOrder)
}
}
return {
data: {
allOrder: allOrder,
waitOrder: waitOrder,
finishOrder: finishOrder,
cancelOrder: cancelOrder,
allOrderNum: allOrderNum,
watiOrderNum: watiOrderNum,
finishOrderNum: finishOrderNum,
cancelOrderNum: cancelOrderNum,
},
errMsg: acc.errMsg,
}
})
}
为什么返回全部,没有给我分类返回?
这么多异步执行,返回结果先后顺序因素考虑了吗
谢谢你的解答,异步执行后,同步完,我再分的类吧,而且返回结果也只给返回了data,根本没返回其他allOrder,waitOrder等分类