This article writes down the micro channel small program cloud development of the entry operation, interested in children’s shoes please go down ~~

Create a small program

  1. Open the applets development tool, create a applets, and choose not to use cloud services

  1. Click the cloud development button in the interface to start the cloud service

The cloud development console is as follows:

Two, operation database

  1. Create a collection

2. Add a recordAdded successfully:3. Obtain dataget()

Write the button in the page and write the click event:

<button bindtap="getdbData">Getting database data</button>
Copy the code

Write the click event in the js file:

// Cloud capability initialization
wx.cloud.init();
// Get the database reference
const db = wx.cloud.database();
Page({
  getdbData() {
    db.collection('test').get().then(res= > {
      console.log(res); })}})Copy the code

Message returned:

We just added a piece of data to the test collection, but did not get it because the data permission was not set. The default data permission isOnly the creator can read and write, when changed toAll users can read, only the creator can read and write, the client can obtain the data.

After the permission is reset, the client can click the button again to obtain data, and the data in the database can be obtained:

  1. Add dataadd()

Add data:

adddbData() {
    db.collection('test').add({
      data: {
        username: "Little red".age: 11
      }
    }).then(res= > {
      console.log(res); })}Copy the code

Return result:

  1. Conditions of the querywhere()
getdbData1() {
    db.collection('test').where({age: 12}).get().then(res= > {
      console.log(res); })}Copy the code

Return result:

  1. The iddoc()
getdbData2() {
    db.collection('test').doc('8937eaa96141b59a0b651c3c3dcef7a9').get().then(res= > console.log(res))
}
Copy the code

Return result:

  1. Update the dataupdate()
updatedbData() {
    const _this = this;
    db.collection('test').where({age: 11}).update({data: {age: 13}}).then(res= > {
      console.log(res) _this.getdbData(); })}Copy the code

Return result:

  1. Delete the dataremove()
removedbData() {
    const _this = this;
    db.collection('test').where({age: 13}).remove().then(res= > {
      console.log(res); _this.getdbData(); })}Copy the code

Return result:

Third, cloud function

  1. Configure the cloud function path:
  • Create a folder cloudFunctions in the root directory
  • In the project. The config. Json SettingscloudfunctionRootfield
  "cloudfunctionRoot": "./cloudFunctions".Copy the code
  1. Create and use cloud functions

Right-click on the cloudFunctions folder, select New Node.js cloud function, and enter the name of the cloud function.

Modify the code in index.js:

// Cloud function entry file
const cloud = require('wx-server-sdk')
// Initialize the cloud function
cloud.init();
exports.main = async (event, context) => {
  // The cloud function returns a value
  return {username: "Zhang"}}Copy the code

After changing the cloud function, right-click in the cloud function folder and select Upload and Deploy to update the cloud function.

Client calls cloud function callFunction() :

getCloudFn() {
    wx.cloud.callFunction({name: "testFn"}).then(res= > {
      console.log(res); })}Copy the code

Return result:

3. Use database in cloud function note:

  • Declare database objects after cloud functions are initialized.
  • The data returned by the cloud function is used on the client sideres.resultTo obtain.
  • Cloud functions are not limited by database permissions or the number of requests.
// Cloud function entry file
const cloud = require('wx-server-sdk')
cloud.init()
// Declare database objects
const db = cloud.database();
exports.main = async (event, context) => {
  return db.collection('test').get().then(res= > {
    returnres; })}Copy the code