云函数
callFunction
1. 接口描述
接口功能:执行云函数
接口声明:callFunction(object: Object): Promise<Object>
2. 输入参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
name | string | 是 | 云函数名称 |
data | object | 否 | 云函数参数 |
callback | function | 否 | 回调函数 |
parse | boolean | 否 | 设置为 true 时,当函数返回值为对象时,API 请求会返回解析对象,而不是 JSON 字符串,适用于在浏览器调试时直接查看返回结果 |
云函数 2.0 额外可以传参数,传入 type:'cloudrun'
参数后,将调用云函数 2.0 服务
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
type | cloudrun | 否 | 是否调用 基于 云托管的云函数 2.0 |
method | string | 否 | HTTP 请求方法 |
path | string | 否 | HTTP 请求路径 |
header | Record<string, string> | 否 | HTTP 请求头 |
data | object | 否 | HTTP 请求体 |
3. 输出参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
code | string | 否 | 状态码,操作成功则不返回 |
message | string | 否 | 错误描述 |
result | object | 否 | 云函数执行结果 |
requestId | string | 否 | 请求序列号,用于错误排查 |
4. 示例代码
import cloudbase from "@cloudbase/js-sdk";
//初始化SDK实例
const app = cloudbase.init({
env: "xxxx-yyy",
});
app
.callFunction({
name: "test",
data: { a: 1 },
})
.then((res) => {
const result = res.result; //云函数执行结果
});
云函数 2.0 示例代码:
import cloudbase from "@cloudbase/js-sdk";
//初始化SDK实例
const app = cloudbase.init({
env: "xxxx-yyy",
});
app
.callFunction({
name: "test",
// 云函数2.0 参数
type: "cloudrun",
method: "POST",
path: "/abc",
data: {
key1: "test value 1",
key2: "test value 2",
},
})
.then((res) => {
const result = res.result; //云函数执行结果
});