This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Cloud development provides several basic capabilities, including databases, storage, cloud functions, cloud calls, and HTTP apis. Here are three basic capabilities: cloud database, cloud storage, and cloud functions.
Experience the cloud Database
Cloud database: Cloud development provides a JSON database, and as the name suggests, each record in the database is a JSON-formatted object. A database can have multiple collections (equivalent to tables in relational data). The collection can be viewed as a JSON array. Each object in the array is a record in the format of a JSON object. In this case, the storage of the database does not need to be considered. A database similar to MongoDB is directly provided, and the free storage space reaches 2G, which is enough for general projects
First add a collection user in the Cloud Development Console > Cloud Database.
Initialize the
By calling const db = wx.cloud.database(); Method to initialize the cloud database.
Add, delete, change and check data
- new: can be called on a collection object
add
Method to insert a record into the collection. - Modify theUse:
update
Method can locally update a record or records in a collection. Locally updating means that only the specified fields are updated, leaving other fields unaffected. - To find the: available on both records and collections
get
Method is used to get data for a single record or multiple records in a collection. - delete: Used for records
remove
Method to delete the record.
// Insert () {db.collection('user').add({data: {name: 'jack', age: 20 } }).then(res => { console.log(res) }).catch(err => { console.log(err) }) }, / / update the update () {the collection (' user '). Doc (' 14139 e126111ea2b042c5a45248db648). Update ({/ / doc () for a _id element data: {name: 'rose' / / need to modify the properties of}}). Then (res = > {the console. The log (res)}). The catch (err = > {the console. The log (err)})}, / / query search () {the collection (' user '). Doc (' 8937 eaa96111efc403ce13e215188f56). The get (). Then (res = > {the console. The log (res) }).catch(err => { console.log(err) }) }, / / delete the delete () {the collection (' user '). Doc (' 8937 eaa96111efc403ce13e215188f56). Remove (). Then (res = > {the console. The log (res) }).catch(err => { console.log(err) }) },Copy the code
Experience the invocation of cloud functions
The sum function
Cloud function definition: The first example is to define a function that adds two numbers. From the shortcut menu, you can choose to create a new Node.js cloudfunction. We will name the cloudfunction “sum”. You can see the cloud function directory and the entry file index.js. We transform it to the return value of sum
Export.main = async (event, context) => {return {sum: event.a + event.b } }Copy the code
The cloud function is passed two parameters, an event object and a context object. Event refers to the event that triggers the cloud function. When the applet side calls the cloud function, event is the parameter passed by the applet side when calling the cloud function, plus the openID of the applet user and the appID of the applet automatically injected by the back end. The Context object contains the invocation information and the running state of the invocation here, and you can use it to see how the service is running. The template also requires wX-server-SDK by default, which is a wechat library that helps us operate the database, store and call other cloud functions in the cloud.
Cloud function call: First deploy the cloud function to the cloud. Right-click on the cloud functions directory. From the right-click menu, we can package the cloud functions as a whole, upload them and deploy them to an online environment.
After deployment, we can call the cloud function from our applet via wx.cloud.callFunction() :
sum: function () {
wx.cloud.callFunction({
name: 'sum',
data: {
a: 1,
b: 2
}
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
}
Copy the code
The final result can be viewed in the debugger as shown below:
Experience the cloud storage capabilities
Cloud development provides a piece of storage space, and provides the ability to upload files to the cloud and download them with permission management. Developers can use the cloud storage function in the small program side and the cloud function side through API. In the small program side, you can call wX.cloud. UploadFile and wX.cloud. DownloadFile respectively to complete the operation of uploading and downloading cloud files. The following uses wX.cloud. uploadFile to realize the function of uploading pictures in the small program and then uploading them to the cloud management. The main principle is as follows:
Implementation steps:
- Click the button to get the local picture;
- Upload local images to the cloud storage system.
- Store picture information in a cloud database.
// upload: function() {wx.choosemessagefile ({count: 10, type: 'image', Success (res) {const tempFilePaths = res.tempfiles // tempFilePath can be used as img tag SRC attribute // upload the image to wx.cloud. UploadFile ({ CloudPath: 'example. PNG ', // Cloud storage path: name uploaded to the cloud storage: filePath: tempFilePaths[0].path, }).then(res => {console.log(res.fileid) // Add fileID to db.collection('images').add({data: { fileID: res.fileID } }).then( res => { console.log(res) }).catch(err => { console.error(err) }) }).catch(error => { console.log(error) }) } }) },Copy the code
Add the image fileID to the cloud database by adding the images collection in the cloud database.
Finally, the corresponding information can be seen in the cloud storage and cloud database
conclusion
This article focuses on the three core basic capabilities of cloud development, but only briefly. For you to have a general understanding of the main capabilities of cloud development, detailed learning should follow the official website of cloud development.
Creation is not easy, click a like and then go ~~~