wx.cloud.callFunction

调用云函数

OBJECT参数说明

参数 类型 必填 说明
name String 云函数名
data Object 传递给云函数的参数
config Object 局部覆写 wx.cloud.init 中定义的全局配置
success Function 返回云函数调用的返回结果
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数/promise返回结果说明:

参数 类型 说明 最低版本
errMsg String 通用返回结果
result String 云函数返回的结果
requestID String 云函数执行 ID,可用于在控制台查找日志 2.3.0

示例代码:

假设已有一个云函数 add

  1. exports.add = (event, context, cb) => {
  2. return event.x + event.y
  3. }

Callback 风格调用

  1. wx.cloud.callFunction({
  2. // 要调用的云函数名称
  3. name: 'add',
  4. // 传递给云函数的参数
  5. data: {
  6. x: 1,
  7. y: 2,
  8. },
  9. success: res => {
  10. // output: res.result === 3
  11. },
  12. fail: err => {
  13. // handle error
  14. },
  15. complete: () => {
  16. // ...
  17. }
  18. })

Promise 风格调用

  1. wx.cloud.callFunction({
  2. // 要调用的云函数名称
  3. name: 'add',
  4. // 传递给云函数的event参数
  5. data: {
  6. x: 1,
  7. y: 2,
  8. }
  9. }).then(res => {
  10. // output: res.result === 3
  11. }).catch(err => {
  12. // handle error
  13. })

原文: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-client-api/functions/callFunction.html