目录

cloudFunction (Cloud Functions Module)

This module provides the capability of calling cloud functions.

Start version: 5.0.0(12)

Module Import

import { cloudFunction } from '@kit.CloudFoundationKit';

call

call(parameters: FunctionParams, callback: AsyncCallback<FunctionResult>): void

Calls a cloud function to use features implemented by the cloud function. This API returns the result asynchronously through a callback.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

Required permission: ohos.permission.INTERNET

System capability: SystemCapability.DeviceCloudGateway.CloudFoundation

Start version: 5.0.0(12)

Parameters:

Parameter

Type

Mandatory

Description

parameters

FunctionParams

Yes

Mandatory parameters for calling a cloud function.

callback

AsyncCallback<FunctionResult>

Yes

Callback function. If the cloud function is called successfully, err is undefined and data is the FunctionResult obtained; otherwise, err is an error object.

Error codes:

For details about the error codes, please refer to ArkTS Error Codes.

Error Code

Error Message

201

No Internet permission.

401

Parameter error.

1008210001

Network connection error.

1008210009

Client internal error.

1008211001

Server error.

Example:
import { cloudFunction } from '@kit.CloudFoundationKit';
import { BusinessError } from '@kit.BasicServicesKit';

cloudFunction.call({
  name:'myhandlerxxxx',
  version:'$latest',
  timeout:10*1000,
  data:{
    param1:'val1',
    param2:'val2'
    }
  }, (err: BusinessError, value: cloudFunction.FunctionResult) => {
    if (err) {
      console.error(`Failed to call the function, Code: ${err.code}, message: ${err.message}`);
      return;
    }
    console.info(`Succeeded in calling the function, result: ${JSON.stringify(value.result)}`);
})

call

call(parameters: FunctionParams): Promise<FunctionResult>

Calls a cloud function to use features implemented by the cloud function. This API returns the result asynchronously through a promise.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

Required permission: ohos.permission.INTERNET

System capability: SystemCapability.DeviceCloudGateway.CloudFoundation

Start version: 5.0.0(12)

Parameters:

Parameter

Type

Mandatory

Description

parameters

FunctionParams

Yes

Mandatory parameters for calling a cloud function.

Returns:

Type

Description

Promise<FunctionResult>

Function call result returned through a promise.

Error codes:

For details about the error codes, please refer to ArkTS Error Codes.

Error Code

Error Message

201

No Internet permission.

401

Parameter error.

1008210001

Network connection error.

1008210009

Client internal error.

1008211001

Server error.

Example:

import { cloudFunction } from '@kit.CloudFoundationKit';
import { BusinessError } from '@kit.BasicServicesKit';

cloudFunction.call({
  name:'myhandlerxxxx',
  version:'$latest',
  timeout:10*1000,
  data:{
    param1:'val1',
    param2:'val2'
  }
}).then((value: cloudFunction.FunctionResult) => {
     console.info(`Succeeded in calling the function, result: ${JSON.stringify(value.result)}`);
}).catch((err: BusinessError) => {
     console.error(`Failed to call the function, Code: ${err.code}, message: ${err.message}`);
})

FunctionParams

Parameters for calling a cloud function.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

System capability: SystemCapability.DeviceCloudGateway.CloudFoundation

Start version: 5.0.0(12)

Name

Type

Read-Only

Optional

Description

name

string

No

No

Cloud function name.

data

string | Object

No

Yes

Function request body.

version

string

No

Yes

Cloud function version. The default value is $latest, indicating the latest version.

timeout

number

No

Yes

Timeout interval for requesting a function, in milliseconds. The default interval is 70 x 1000 milliseconds.

The value range is unrestricted, and the value will be converted to the unsigned long type.

loadMode

LoadMode

No

Yes

Function loading mode. The default value is NORMAL.

LoadMode

Enumerated function loading modes.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

System capability: SystemCapability.DeviceCloudGateway.CloudFoundation

Start version: 5.0.0(12)

Name

Value

Description

NORMAL

0

Normal mode, in which the server is requested to obtain data each time the call method is called.

PRELOAD

1

Preload mode, in which the result data is returned only once when the app is installed. An error will be returned when the call method is called later.

FunctionResult

Result returned by the cloud function.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

System capability: SystemCapability.DeviceCloudGateway.CloudFoundation

Start version: 5.0.0(12)

Name

Type

Read-Only

Optional

Description

result

string | Object

No

No

Result returned by the cloud function.