May 20, 2021 WeChat Mini Program Development Document
Support side: small programs, cloud functions
Call the cloud function
Property | Type | The default | Required | Description |
---|---|---|---|---|
name | string | Is | The name of the cloud function | |
data | Object | Whether |
Arguments passed to the cloud function, which can be obtained through
event
parameter in the cloud function
|
|
config | Object | Whether | Configuration | |
success | function | Whether | The interface calls a successful callback function | |
fail | function | Whether | The interface calls the failed callback function | |
complete | function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
The structure of object.config
Property | Type | The default | Required | Description |
---|---|---|---|---|
Env | string | Is | Environment ID, which is specified when init is ignored when filled in |
Property | Type | Description |
---|---|---|
result | any | The result returned by the cloud function |
requestID | string | The cloud function executes the ID and can be used for log queries |
If data contains a big data field (recommended threshold 256KB), it is recommended to mark the big data field with wx.cloud.CDN, which is then uploaded to the temporary CDN when the cloud function is called, and then the field value received in the cloud function will be CDN url, which can be downloaded and accessed in the cloud function. In this way, you can avoid performance issues caused by big data transfers and avoid touching the transfer size limits of the calling link.
If data of the Buffer type is passed in in data, the data will be converted to the format of "type": "Buffer", data: number, in the case of a small terminal call, during JSON serialization:
// 小程序端调用
wx.cloud.callFunction({
// ...
data: {
buf: ArrayBuffer // 此处填入了某种方式获取得到的 Buffer 数据,可以是 request 下来的,可以是读文件读出来的等等
},
})
// 云函数端收到的 event 参数的结构:
{
"type": "Buffer",
"data": [ 17, 371, 255, ... ] // Uint8 Array
}
Therefore, you should avoid passing in data of the Buffer type because it increases the volume of the data, increases the transfer time, and there are two alternatives to passing buffer if you need to:
Suppose you already have a cloud function add:
exports.add = (event, context, cb) => {
return event.x + event.y
}
Make a call to the cloud function add on the small terminal:
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'add',
// 传递给云函数的event参数
data: {
x: 1,
y: 2,
}
}).then(res => {
// output: res.result === 3
}).catch(err => {
// handle error
})
Make a call to the cloud function add at any cloud function on the cloud function side (full example of cloud function code):
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
const res = await cloud.callFunction({
// 要调用的云函数名称
name: 'add',
// 传递给云函数的参数
data: {
x: 1,
y: 2,
}
})
// 3
return res.result
}
Small terminal callback style calls:
The small terminal also supports Callback-style calls, such as called on Promise, which can be rewritten in Callback-style:
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'add',
// 传递给云函数的参数
data: {
x: 1,
y: 2,
},
success: res => {
// output: res.result === 3
},
fail: err => {
// handle error
},
complete: () => {
// ...
}
})