Applet cloud function timing trigger

WeChat applet cloud function call to clear part of the specified data in the database

First configure the time function tim in json

{
  "permissions": {
    "openapi": [
    ]
  },
  
    
  "triggers": [
    {
    
      "name": "myTriggerxx",
      
      "type": "timer",
      
      "config": "10 0 0 * * * *"
    }
  ]
  
}

The type must be timer, otherwise the time function cannot be triggered

Among them, the config is arranged according to the second, minute and hour

No specified * can be

After the configuration is complete, upload the trigger, and then create and deploy cloud function dependencies (you can find it by right-clicking on the file name)

Once you're done, you can start a timing trigger.

Then configure the cloud function settings

exports.main = async (event, context) => {
  return cloud.database().collection('ydbsjh').where({}).update({
    // data 传入需要局部更新的数据
    data: {
      // 表示增加的字段
      ydnumbers:0,
      jrdk:0
    },
    success: function(res) {
      console.log(res)
    }
  })
  

}

It is the same as the basic database operation, update operation, update is OK, where is empty means that all databases will be updated, if there is something that needs to be specified, fill it in, it is the same as the basic database operation

This is the case for clearing. If there is something to be cleared, it is to delete the field instead of updating. It is the same as the database operation. I will not paste the code segment here.

Return print information console.log after success

In this way, the timing trigger of the cloud function can be realized.

At the same time, remember to set the function environment. If this environment is not set, it is easy to report an error.

const cloud = require('wx-server-sdk')

cloud.init({
  env:"cloud1-4g9fahjha162869b"}
)

Guess you like

Origin blog.csdn.net/m0_58609505/article/details/127282285