微信小程序> 20个优秀微信小程序-微信小程序读取数据超过20,100的限制方法-微信有哪些小程序

20个优秀微信小程序-微信小程序读取数据超过20,100的限制方法-微信有哪些小程序

浏览量:1549 时间: 来源:黑鸦log

1.首先,无论小程序端单次读取数据库最多20条,云函数单次读取数据库最多100条,这是官方限制,是无法突破的,但是如果你能黑进TX改限制,那我倒头便拜。

2.所以解决方案就是把单次查询分解成若干次

云函数端1.首先获得欲查询数据的总数asyncfunctiongetCountIndexUserId(userId){letcount=awaitdb.collection('info').where({"userId":userId}).count();returncount;}

3.获取“info”集合中所有“userId”的值为指定值userId的总数

2.然后编写单次查询函数

4.单次查询数据总量不超过100

asyncfunctiongetListIndexUserId(userId,skip){letlist=awaitdb.collection('info').where({"userId":userId}).orderBy('_id','asc').skip(skip).get();returnlist.data;}

5.获取“info”集合中所有“userId”的值为指定值,序号从skip到skip+100的数据skip使用说明详见官方文档

3.循环查询,然后组合到一起

6.云函数的main函数如下:

exports.main=async(event,context)={letcount=awaitgetCountIndexUserId(event.userId);count=count.total;letlist=[]for(leti=0;icount;i+=100){list=quesionList.concat(awaitgetListIndexUserId(event.userId,i));}returnlist;}

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

小程序端

8.思路与云函数端一样,不同的是小程序端不允许await,需要用Promise方法实现

1.获取总数functiongetListCount(openid){returnnewPromise((resolve,reject)={db.collection('info').where({"studyUserId":openid}).count().then(res={resolve(res.total);}).catch(e={console.log(e)reject("查询失败")})})}

9.这里查询数据库集合“info”中“studyUserId”的值为给定值“openid”的所有数据总数

2.单次查询函数functiongetListIndexSkip(openid,skip){returnnewPromise((resolve,reject)={letstatusList=[]letselectPromise;if(skip0){selectPromise=db.collection('info').where({"studyUserId":openid}).skip(skip).get()}else{//skip值为0时,会报错selectPromise=db.collection('info').where({"studyUserId":openid}).get()}selectPromise.then(res={resolve(res.data);}).catch(e={console.error(e)reject("查询失败!")})})}

10.需要注意的是,小程序端的数据库查询不允许skip设置为0,否则会报错,有哪位大神知道TX为什么这么设计,还请赐教

3.循环查询,整合数据getListCount(openid).then(res={letcount=resletlist=[]for(leti=0;icount;i+=20){getListIndexSkip(openid,i).then(res={list=list.concat(res);if(list.length==count){resolve(list)}}).catch(e={console.error(e)reject("查询失败")})}})})

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎