[Applet - Cloud Development] Hands-on with Cloud Development (Database Development)
Following the last time the programmer brother simply developed a photo storage applet, I feel that there are still some minor flaws, that is, there is no way to upload photos to the text description. Since it's mostly text, it's obviously not a good idea to save the text descriptions as files to storage space. Well then, let's work together today on the database that comes with cloud development; it should solve this little flaw of ours.
The database that comes with Cloud Development is a bit different from the usual relational databases that you come across. The database provided by Cloud Development is a JSON database, where each record is a JSON formatted object. A database can have multiple collections (equivalent to data tables in relational data), and a collection can be thought of as a JSON array, where each object in the array is a record, and the records are formatted as JSON objects. To put it simply, each record is a JSON, so you don't have to think about building tables and fields.
Isn't it amazing~ as well advertised as it is, you need to look at the features. We're going to build on the photo storage applet we built last time into the extension step by step below.
Click on Cloud Development, click on "Database" on top of the Cloud Development console and then click on "Add Collection"
Adding a collection here is equivalent to building our library (there has to be somewhere to store the data), and with that we are done with the cloud database. Next we just add the data to this collection via the applet.
Since we have to write data to the collection in the database, then the idea is that the photos are uploaded successfully and then the logs are filled in and then saved to the collection. So we're going to create a new page for writing to the log and saving it.
In front of the new page, we want to upload the page before the transformation, that is, after a successful upload plus a page with a file ID jump (wx.navigateTo) to our new record page, the following figure.
Here I have created a simple new record page, mainly a display of the incoming image and text input box and a submit button, where fileid is the incoming file ID. The wxml code is as follows:
Key point:Because we want to receive the file ID value returned after a successful upload, we have to add a receive function to onload. As shown below.
Next, let's see how to operate on the cloud database, The first step is definitely to connect to our cloud database can be done viaconst db = wx.cloud.database() come to a connection, Then it writes data to the corresponding data setdb.collection(' The data set says').add()。 The code is as follows:
Everything is ready for commissioning. When we can see the JSON record we have written in the database in the cloud development controller, so that the data is successfully written to the cloud database.
-END-
Note: This article has been first published in [WeChat Open+] in the [small app community]
For more technical articles you can scan the QR code below to join WeChat Open+