目录

cloudFunction (云函数模块)

本模块提供调用云函数的能力。

起始版本:5.0.0(12)

导入模块

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

call

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

调用云函数时使用,使用云函数实现的功能。使用callback异步回调。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

需要权限:ohos.permission.INTERNET

系统能力:SystemCapability.DeviceCloudGateway.CloudFoundation

起始版本:5.0.0(12)

参数:

参数名

类型

必填

说明

parameters

FunctionParams

是

云函数调用必备参数。

callback

AsyncCallback<FunctionResult>

是

回调函数。当调用云函数成功,err为undefined,data为获取到的FunctionResult;否则为错误对象。

错误码:

以下错误码的详细介绍请参见ArkTS错误码。

错误码ID

错误信息

201

No Internet permission.

401

Parameter error.

1008210001

Network connection error.

1008210009

Client internal error.

1008211001

Server error.

示例:
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>

调用云函数时使用,使用云函数实现的功能。使用Promise异步回调。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

需要权限:ohos.permission.INTERNET

系统能力:SystemCapability.DeviceCloudGateway.CloudFoundation

起始版本:5.0.0(12)

参数:

参数名

类型

必填

说明

parameters

FunctionParams

是

云函数调用必备参数。

返回值:

类型

说明

Promise<FunctionResult>

Promise对象,返回函数调用结果。

错误码:

以下错误码的详细介绍请参见ArkTS错误码。

错误码ID

错误信息

201

No Internet permission.

401

Parameter error.

1008210001

Network connection error.

1008210009

Client internal error.

1008211001

Server error.

示例:

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

云函数调用参数。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.DeviceCloudGateway.CloudFoundation

起始版本:5.0.0(12)

名称

类型

只读

可选

说明

name

string

否

否

云函数名称。

data

string | Object

否

是

函数请求体。

version

string

否

是

云函数版本,默认是‘$latest’,表示最新版本。

timeout

number

否

是

函数请求超时时间,单位毫秒,默认为70秒。

loadMode

LoadMode

否

是

函数请求加载模式,默认为NORMAL。

LoadMode

枚举, 函数请求加载模式。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.DeviceCloudGateway.CloudFoundation

起始版本:5.0.0(12)

名称

值

说明

NORMAL

0

普通模式,每次调用call方法均需请求服务器拉取数据。

PRELOAD

1

预加载模式,仅在安装应用时返回一次结果数据,后续调用将返回错误。

FunctionResult

云函数返回的结果。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.DeviceCloudGateway.CloudFoundation

起始版本:5.0.0(12)

名称

类型

只读

可选

说明

result

string | Object

否

否

云函数返回的结果。