Let’s start with the small program we finally developed using the cloud. The small program code is as follows:
Small program name for the home museum, mainly used to manage home items. It involves user account system, item management, classification and search and other functions. It uses cloud function, database, storage, CMS content management and other capabilities of cloud development.
Start fast
If you are not familiar with cloud development, you can first follow the official documents, quickly create a small program for cloud development for reference. The document does not need to see too much, the small program can run up (cloud development of more documents, the following will be involved in the content will give the corresponding document links).
Since the small program we are developing involves databases, cloud functions, and cloud storage, we will follow these sections.
Ii. Database
2.1 Content management system
Handwritten table structure is a bit slow, and careless and can make mistakes, so with the help of a content management system, this can greatly improve the efficiency of work. For details, go to CloudBase CMS.
Using the content management system above, we can easily manage the content model (be careful not to change the name of the content model, otherwise there will be problems in the content section, so choose the name carefully), but also create the content.
2.2 Database of cloud development panel
In addition to the content management system above, we can also manage it in a database in the cloud development panel. The diagram below:
In addition, advanced operations also provide some database operation instances, as shown below:
2.3 Adding, deleting, modifying and checking databases
Keep in mind that there are three steps to operating the database (we will continue with the specific instance operation in the cloud function) :
- Select the database for which environment
Const DB = wx.cloud.database({env: 'test' // which environment});Copy the code
- Which set to choose
const users = DB.collection('users');
Copy the code
- Add, delete, modify and check the collection
const user = users.doc('_id');
Copy the code
For more detailed documentation, refer to the DATABASE SDK
2.4 document ID
In the content management system, the document ID is a system field and can only be generated automatically. However, in some cases, we would like to be able to customize the document ID, such as the uniform classification of data.
Fortunately, the cloud Development Panel database supports customization, so if you really need a custom document ID, you can define it directly in the cloud development Panel database. However, the cloud development panel database custom ID of that field input box, there is a length limit.
The document ID is useful when querying a single data record, such as getting information about a user:
// If openID is not found, the user is not registered. // If openID is not found, the user is not registered. Users. Doc (' openid). The get (), then ((res) = > {the console. The log (res) data)}). The catch ((e) = > {the console. The log (' not registered ')});Copy the code
Third, cloud function
3.1 Implement the first cloud function
First we implement our first cloud function against my first cloud function document.
There is one library and two apis to note:
- One library document: WX-server-SDK
- Two API documents: getWXContext (it is important to note that different calls may return different data) and callFunction
Next, we can follow the documentation for cloud functions until we see the local debugging.
With that in mind, we can begin cloud function development in earnest. The following uses the user cloud function as an example.
3.2 Actual cloud function
- Let’s start by creating a new node.js cloud function (right click on the root directory of the cloud function and select Create a new Node.js cloud function named user from the right click menu).
- Install dependency files
Right-click the user folder, open it on the built-in terminal, and run the NPM I command to install the dependency file.
- Enable local debugging of cloud functions
After the dependency files are installed, right-click the user folder and select Enable local debugging of cloud functions.
The local debug panel for cloud functions opens as follows. Note the check on the right.
- Writing cloud functions
The overall code looks like this (depending on the type of data to request) :
- Small program side call
First, complete the cloud capability initialization in app.js with the code as follows: (Refer to the small program side Cloud capability initialization document).
App({ onLaunch() { if (! Wx.cloud) {console.error(' Please use base libraries 2.2.3 or above to use cloud capabilities '); } else { wx.cloud.init({ env: 'cloud1-xxx', traceUser: true, }); }}});Copy the code
Call the cloud function user where needed, as follows:
Wx.cloud. callFunction({// cloud function name: 'user', // data: {type: 'get' }, }) .then(res => { console.log(res.result) }) .catch(console.error)Copy the code
- Upload and deploy
After debugging and development, you can upload and deploy it, as shown below:
3.3 Cloud Function Management
All of our cloud functions can be managed through the Cloud Development panel, as shown below:
Iv. Cloud storage
In fact, in the fast start inside, the default creation of a small program inside the upload picture of a cloud development instance, to the inside of the instance copy again, print out some information to see will use.
For details, see File Storage.
Of course, for the user uploaded pictures, it is best to have a cropping function, small program cropping picture components are also a lot of online, find a suitable yourself.
For the stored content, we can also view it through the cloud development panel, as shown in the figure below:
5. Open data
Cloud development also provides a new way to invoke open data: open data validation and decryption.
Let’s take getting a phone number as an example.
- use
button
Components,open-type
为getPhoneNumber
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
Copy the code
- in
getPhoneNumber
Get in thecloudID
- Write the cloud function user and call the getOpenData API. The main code is:
const { type, cloudID } = event; If (type === 'getPhone') {const res = await cloud.getopendata ({list: [cloudID],}); const resPhone = res.list[0].data.phoneNumber; return resPhone; }Copy the code
- The applet side calls the cloud function, and you get the phone number.
Wx.cloud. callFunction({// cloud function name: 'user', // data: {type: },}).then(res => {console.log(res.result)}).catch(console.error)Copy the code
Six, summarized
In general, the small program cloud development has rounded our full stack dream, a person a shuttle is happy, but in the process of groping is actually a challenge, cloud development continues to develop and optimize, we need to keep pace ~
Source: Tencent IMWEB team
Product introduction
Tencent CloudBase (TCB) is a cloud native integrated development environment and tool platform provided by Tencent Cloud. It provides developers with highly available, automatic and elastic back-end cloud services, including computing, storage, hosting and other serverless capabilities. It can be used for cloud integrated development of multiple end applications (small programs, Public account, Web application, Flutter client, etc.), helps developers to build and manage back-end services and cloud resources in a unified manner, avoiding cumbersome server construction and operation and maintenance in the application development process. Developers can focus on the implementation of business logic, with lower development threshold and higher efficiency.
Open cloud development: console.cloud.tencent.com/tcb?tdl_anc…
The product documentation: cloud.tencent.com/product/tcb…
Technical documentation: cloudbase.net? From =10004
Technical exchange group, latest information follow wechat public account [Tencent Cloud development CloudBase]