当前位置:网站首页>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 .

《 Used in applet async and await The mutation step is synchronization , Solve the problem of callback to hell 》

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 .

原网站

版权声明
本文为[Programming pebbles]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210628163530618f.html