收藏
回答

微信小程序云函数返回值问题请给位帮我看看,这个返回值为什么是返回全部,却没有给我

框架类型 问题类型 终端类型 AppID 基础库版本
小程序 Bug 客户端 wx26799427a6e4f382 2.2.5

- 当前 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,

}

})

}


回答关注问题邀请回答
收藏

1 个回答

  • 鸿
    鸿
    2019-06-27

    为什么返回全部,没有给我分类返回?

    2019-06-27
    有用
    回复 2
    • \
      \
      2019-06-27

      这么多异步执行,返回结果先后顺序因素考虑了吗

      2019-06-27
      回复
    • 鸿
      鸿
      2019-06-27回复\

      谢谢你的解答,异步执行后,同步完,我再分的类吧,而且返回结果也只给返回了data,根本没返回其他allOrder,waitOrder等分类

      2019-06-27
      回复
登录 后发表内容