当前位置:网站首页>The applet reads more than 20 data, and the cloud function reads more than 100 restrictions
The applet reads more than 20 data, and the cloud function reads more than 100 restrictions
2022-06-24 08:08:00 【Programming pebbles】
Students who have seen the basic course of stone Geyun development must know , Request data directly in the applet, and you can only return at most 20 Data , The requested data in the cloud function can only return at most 100 Data , If you want to break through this limit , The best way is to do paging , Of course, I also teach you in Cloud Development Foundation . But there is a need to limit , What if we want to get all the data stored in the database at one time , For example, there are... In the database 1000 Data , We want to get it all at once , How do you do that ???
Today, let's teach you how to return up to... Each time through the cloud function 100 Restrictions on .
One , Cloud function breakthrough 100 Restrictions on
As shown in the figure above , my num There are 103 Data , If you follow the previous pagination idea , It's two requests , First request 1-100 strip , Second request 101-103 strip . So we can get it 103 Data , But it takes two requests , How do we do it , Only one request can get this information 103 Data .
1-1, breakthrough 100 Principle of bar
In fact, the principle is the same as our paging principle , It's just that we use for Loop multiple requests , Then combine the multiple requests together , Then return the combined data all at once . In this way, all data is obtained through one request .
1-2, Code implementation
The code here is written in the cloud function , I'll write down the operation steps in the notes .
As shown in the figure above , We only need to go through three steps , You can get everything 103 Bar data .
So we can pass a request , Got all the data . Does it feel very simple . Here the code is posted to everyone .
// Cloud function entry file
const cloud = require('wx-server-sdk')
// Cloud development environment initialization
cloud.init({env: cloud.DYNAMIC_CURRENT_ENV})
exports.main = async (event, context) => {
const db = cloud.database()
// 1, Total number of acquired data
let count = await db.collection('num').count()
count = count.total
// 2, adopt for Loop multiple requests , And put the data requested many times into an array
let all = []
for (let i = 0; i < count; i += 100) { // Set the amount of data obtained each time
let list = await db.collection('num').skip(i).get()
all = all.concat(list.data);
}
// 3, Return all the assembled data at one time
return all;
}1-3, matters needing attention
The data returned by a cloud function cannot exceed 1M, If you need more than 1M, You need to use the data query on the applet side 20 strip 20 The of bars are combined . So if you want to return a lot of data at once , You can consider requesting the database directly in the applet , Then I do 20 strip 20 Assembly of bar .
I will also teach you how to assemble in small programs .
Two , Applet directly requests database breakthrough 20 strip
2-1, Turn on async and await
We won't talk about the principle here , It's as like as two peas. , But in small programs await You need to do the following simple operations , You can also read another article of mine .
It seems that the latest version of the applet developer tool supports async and await The method , It seems that it's OK not to check enhanced compilation . But for safety's sake , It's better to check enhanced compilation .
2-2, Authority modification
Get the data in the database directly from the applet , Remember to change the permissions of the collection
2-3, Code implementation
Careful students can certainly see it , The above code is basically as like as two peas in our cloud function .
Here we have perfectly broken through the limitation of small programs , Return any number of data we want .
Later, we will summarize more small program knowledge points for you , Welcome to your attention , Welcome to leave a message .
边栏推荐
- [data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
- decltype用法介绍
- Error "computing failed in `stat\u summary\u hex() `"
- Thread considerations
- Swift Extension NetworkUtil(網絡監聽)(源碼)
- Phonics
- LINQ 查询(2)
- OC extension detects whether an app is installed on the mobile phone (source code)
- Introduction to software engineering - Chapter 2 - feasibility study
- Hongmeng development IV
猜你喜欢
随机推荐
The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain
Pipeline concept of graphic technology
没有专业背景,还有机会成为机器学习工程师吗?
某问答社区App x-zse-96签名分析
Using kubeconfig files to organize cluster access
单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案
. No main manifest attribute in jar
毕业两年月薪36k,说难也不难吧
Gossip: what happened to 3aC?
GraphMAE----论文快速阅读
4-操作列表(循环结构)
【资料上新】迅为基于3568开发板的NPU开发资料全面升级
OC extension detects whether an app is installed on the mobile phone (source code)
Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
Atguigu---15- built in instruction
Notes on the use of date and time base
Thread support
Thread blocking
Model effect optimization, try a variety of cross validation methods (system operation)









