You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
clothesProject/cloudfunctions/get/index.js

32 lines
944 B

2 years ago
// 云函数入口文件
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('cloth').count()
const total=countResult.total
//计算分几次取
const batchTimes = Math.ceil(total / 100)
// 承载所有读操作的 promise 的数组
const tasks = []
for (let i = 0; i < batchTimes; i++) {
const promise = db.collection('cloth').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
tasks.push(promise)
}
// 等待所有
return (await Promise.all(tasks)).reduce((acc, cur) => {
return {
event,
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
data: acc.data.concat(cur.data),
errMsg: acc.errMsg,
}
})
}