跳到主要内容

云函数

callFunction

1. 接口描述

接口功能:执行云函数

接口声明:callFunction(object: Object): Promise<Object>

2. 输入参数

字段类型必填说明
namestring云函数名称
dataobject云函数参数
callbackfunction回调函数
parseboolean设置为 true 时,当函数返回值为对象时,API 请求会返回解析对象,而不是 JSON 字符串,适用于在浏览器调试时直接查看返回结果

云函数 2.0 额外可以传参数,传入 type:'cloudrun' 参数后,将调用云函数 2.0 服务

字段类型必填说明
typecloudrun是否调用 基于 云托管的云函数 2.0
methodstringHTTP 请求方法
pathstringHTTP 请求路径
headerRecord<string, string>HTTP 请求头
dataobjectHTTP 请求体

3. 输出参数

字段类型必填说明
codestring状态码,操作成功则不返回
messagestring错误描述
resultobject云函数执行结果
requestIdstring请求序列号,用于错误排查

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; //云函数执行结果
});