# wx.cloud.callFunction

Calls cloud function.

OBJECT parameter description

Parameter Type Required Description
name String Yes Cloud function name
data Object No Parameter passed to the cloud function
config Object No Locally overwrite the global configuration defined in wx.cloud.init
success Function No Returns the result of cloud function call
fail Function No Callback function for failed API call
complete Function No Callback function used when API call completed (always executed whether call succeeds or fails)

config object definition

Field Description Data Type
env ID of the used environment. Ignore the environment specified by init if this parameter is specified. String

Description of success return parameter/promise return result:

Parameter Type Description Minimum Version
errMsg String General return result
result String Return result of cloud function call
requestID String The cloud function execution ID, used to query logs in the console 2.3.0

Sample code:

Suppose a cloud function add already exists:

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

Callback-style call

wx.cloud.callFunction({
  // The name of the cloud function to be called
  name: 'add',
  // Parameter to be passed to the cloud function
  data: {
    x: 1,
    y: 2,
  },
  success: res => {
    // output: res.result === 3
  },
  fail: err => {
    // handle error
  },
  complete: () => {
    // ...
  }
})

Promise-style call

wx.cloud.callFunction({
  // The name of the cloud function to be called
  name: 'add',
  // The event parameter to be passed to the cloud function
  data: {
    x: 1,
    y: 2,
  }
}).then(res => {
  // output: res.result === 3
}).catch(err => {
  // handle error
})