完全使用官方获取大于100条数据的代码
谁帮忙看看啥意思
const cloud = require('wx-server-sdk')
// 初始化 cloud
cloud.init({
env: "lghcloud2-fnker",
traceUser: true
})
const db = cloud.database({ env: "lghcloud2-fnker"})
const MAX_LIMIT = 100
exports.main = async (event, context) => {
var group=event.group
var admincompany=event.admincompany
const countResult = await db.collection(group).where({ usercompany: admincompany, order:false }).count()
const total = countResult.total
console.log("total",total)
// 计算需分几次取
const batchTimes = Math.ceil(total / 100)
// 承载所有读操作的 promise 的数组
const tasks = []
for (let i = 0; i < batchTimes; i++) {
const promise = db.collection(group).where({ usercompany:admincompany, order:false }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
tasks.push(promise)
console.log("TASK",tasks)
}
console.log("task",tasks)
// 等待所有
return (await Promise.all(tasks)).reduce((acc, cur, [, initialValue]) => {
return {
event,
data: acc.data.concat(cur.data),
errMsg: acc.errMsg,
}
})
}
//上面为失败代码
//以下为修改后的代码,亲测可用
exports.main = async (event, context) => {
const group=event.group
const admincompany=event.admincompany
let count =await getCount(admincompany,group);
count = count.total;
let data =[]
for (let i = 0; i < count; i += 100) {//自己设置每次获取数据的量
data = data.concat(await getList(i,admincompany,group));
}
console.log("list",data)
return data;
}
async function getCount(admincompany,group) {//获取数据的总数,这里记得设置集合的权限
let count = await db.collection(group).where({usercompany:admincompany, order:false
}).count();
return count;
}
async function getList(skip,admincompany,group) {//分段获取数据
let list = await db.collection(group)
.where({usercompany:admincompany, order:false
}).skip(skip).get();
return list.data;
}
亮代码