Skip to main content

Cloud Function

callFunction

1. Interface Description

Function: Execute cloud function

Interface declaration: callFunction(object: Object): Promise<Object>

2. Input Parameters

FieldTypeRequiredDescription
namestringYesCloud function name
dataobjectNoCloud function parameters
callbackfunctionNoCallback function
parsebooleanNoWhen set to true, if the function returns an object, the API will return the parsed object instead of a JSON string. This is applicable for directly viewing the returned results during browser debugging.

Function Cloud Hosting can additionally pass parameters. After passing the type:'cloudrun' parameter, the Function Cloud Hosting service will be invoked.

FieldTypeRequiredDescription
typecloudrunNoWhether to invoke the function-based cloud hosting service
methodstringNoHTTP request method
pathstringNoHTTP request path
headerRecord<string, string>NoHTTP request headers
dataobjectNoHTTP request body

3. Output Parameters

FieldTypeRequiredDescription
codestringNoStatus code, not returned if the operation is successful
messagestringNoError description
resultobjectNoCloud function execution result
requestIdstringNoRequest ID, used for error troubleshooting

4. Sample Code

import cloudbase from "@cloudbase/js-sdk";
//Initialize the SDK instance
const app = cloudbase.init({
env: "xxxx-yyy",
});

app
.callFunction({
name: "test",
data: { a: 1 },
})
.then((res) => {
const result = res.result; // Cloud function execution result
});

Function Cloud Hosting Sample Code:

import cloudbase from "@cloudbase/js-sdk";
//Initialize the SDK instance
const app = cloudbase.init({
env: "xxxx-yyy",
});

app
.callFunction({
name: "test",
// Function Cloud Hosting parameters
type: "cloudrun",
method: "POST",
path: "/abc",
data: {
key1: "test value 1",
key2: "test value 2",
},
})
.then((res) => {
const result = res.result; // Cloud function execution result
});