受欢迎的博客标签

微信小程序云开发--小程序云函数-云函数读取数据超过20,100的限制方法

Published

1.云函数端

1.1. 逐条取数

新建云函数syncToLocalDatabase

// 云函数入口文件
const cloud = require('wx-server-sdk')


cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

//定义要备份的集合
const Employee = "Employee";
const ProductStockForShop = "ProductStockForShop";
const ProductStockForChannel = "ProductStockForChannel";
const ProductStockMonthForChannel = "ProductStockMonthForChannel";


// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  const db = cloud.database()

  var result;
  // step 1..首先获得欲查询数据的总数
  result = await db.collection(Employee).count();
  console.log('调用云函数[syncToLocalDatabase] 获取的记录总数:' + Employee + ' result=' + JSON.stringify(result))
  let count = result.total;

  // step 2.一条条获取记录,(云函数单次返回的数据不能超过1M,就1条条读取),实际返回是[]格式的集合,缺省时limit=1
  for (let i = 0; i < count; i += 1) {
    let records = await db.collection(Employee).skip(i).get();
    console.log('调用云函数[syncToLocalDatabase] 获取的记录:' + Employee + '第' + i + '条' + JSON.stringify(records))

  }


  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}

 

1.2 批量取数方法1

step 1..首先获得欲查询数据的总数

async function getCountIndexUserId(userId) {
  let count = await db.collection('info').where({
    "userId": userId
  }).count();
  return count;
}

step 2.然后编写单次查询函数
单次查询数据总量不超过100

async function getListIndexUserId(userId,skip) {
  let list = await db.collection('info').where({
    "userId": userId
  }).orderBy('_id', 'asc').skip(skip).get();
  return list .data;
}

 

step 3.循环查询,然后组合到一起
云函数的main函数如下:

exports.main = async (event, context) => {
  let count = await getCountIndexUserId(event.userId);
  count = count .total;
  let list=[]
  for (let i = 0; i < count ; i += 100) {
    list = quesionList.concat(await getListIndexUserId(event.userId, i));
  }
  return list ;
}

云函数单次返回的数据不能超过1M,如果需要超过1M,则需要使用小程序端的数据查询20条20条的进行组合了

 

微信小程序读取数据超过20,100的限制方法(云函数端和小程序端)

https://blog.csdn.net/c0411034/article/details/100533151

1.3 批量取数方法2

微信小程序云数据库获取集合所有数据.

小程序端与云函数端的表现会有如下差异:

小程序端:如果没有指定 limit,则默认且最多取 20 条记录。
云函数端:如果没有指定 limit,则默认且最多取 100 条记录。
获取集合中的所有待办事项清单:因为有默认 limit 100 条的限制,因此很可能一个请求无法取出所有数据,需要分批次取:

云函数端

// 云函数入口文件
const cloud = require('wx-server-sdk')
​
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
  throwOnNotFound: false
})
const db = cloud.database()
const MAX_LIMIT = 2
// 云函数入口函数
exports.main = async (event, context) => {
 // 先取出集合记录总数
 const countResult = await db.collection('test').count()
 console.log(countResult)
 const total = countResult.total
 // 计算需分几次取
 const batchTimes = Math.ceil(total / MAX_LIMIT)
 // 承载所有读操作的 promise 的数组
 const tasks = []
 for (let i = 0; i < batchTimes; i++) {
   //get()操作返回的是Promise对象,每获取一个Promise就压栈进入tasks数组
   const promise = db.collection('test').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
   tasks.push(promise)
 }
 console.log(tasks)
 console.log(await Promise.all(tasks))
 // 等待所有
 /* Promise.all 方法用于将多个 Promise 实例,包装成一个新的 Promise 实例
  在任何情况下,Promise.all 返回的 promise 的完成状态的结果都是一个数组。
  在这里,返回的数组的元素就是res.data
  数组reduce操作:array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
  total  必需。初始值, 或者计算结束后的返回值。
  currentValue  必需。当前元素
  currentIndex  可选。当前元素的索引
  arr  可选。当前元素所属的数组对象。
  initialValue  可选。传递给函数的初始值
  **此处acc为初始值,cur为当前元素
  concat() 方法用于连接两个或多个数组
 */
 return (await Promise.all(tasks)).reduce((acc, cur) => {
   return {
     data: acc.data.concat(cur.data),
     errMsg: acc.errMsg,
   }
 })
}