function.md 1.5 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
## callFunction(options) @callfunction

<!-- UTSUNICLOUDAPIJSON.callFunction.description -->

<!-- UTSUNICLOUDAPIJSON.callFunction.param -->

<!-- UTSUNICLOUDAPIJSON.callFunction.returnValue -->

<!-- UTSUNICLOUDAPIJSON.callFunction.compatibility -->

<!-- UTSUNICLOUDAPIJSON.callFunction.tutorial -->

<!-- UTSUNICLOUDAPIJSON.unicloud-call-function.example -->
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

### 调用云函数时传入泛型

> 4.13版本起支持,仅安卓端会构造对应的泛型的实例,web端和iOS端泛型仅作为类型使用。

用法:`uniCloud.callFunction<泛型类型>({name: 'xxx', data: {}} as UniCloudCallFunctionOptions)`

在不传泛型时callFunction返回的类型为`Promise<UniCloudCallFunctionResult<UTSJSONObject>>`,传入泛型后callFunction返回的类型为`Promise<UniCloudCallFunctionResult<泛型类型>>`

**代码示例**

```ts
// 云函数fn代码
'use strict';
exports.main = async (event, context) => {
  return {
    errCode: 0,
    errMsg: '',
    detail: 'call function detail'
  };
};
```

```ts
// 客户端代码
type CallFunctionResult = {
  errCode : number,
  errMsg : string,
  detail : string
}
uniCloud.callFunction<CallFunctionResult>(
  {
    name: 'fn',
    data: { a: 1 } as UTSJSONObject,
  } as UniCloudCallFunctionOptions
).then(function (res) {
  const result = res.result // result的类型为CallFunctionResult
  const detail = result.detail // 可直接使用.detail访问
  console.log(detail)
}).catch(function (err : any | null) {
  console.error(err)
})
```