---
name: "insertRecords"
mode: "kit"
versionRequirements:
  - { name: "HealthKit", version: "5.18.0" }
  - { name: "@ray-js/ray", version: "1.6.17" }
platform:
  - "Android"
async: true
title: "health.insertRecords - 数据同步到 health connect [Android only]"
---

## insertRecords

> [VERSION] HealthKit >= 5.18.0 | @ray-js/ray >= 1.6.17

> [PLATFORM] Android

> ⚡ **支持 Promise 调用** — 不传 success / fail / complete 回调时，该方法返回 Promise。

### 描述

将健康记录批量写入 Health Connect [Android only]

### 参数

| 属性 | 类型 | 必填 | 默认值 | 最低版本 | 描述 |
| --- | --- | --- | --- | --- | --- |
| `request` | `string` | 是 | - | `5.18.0` | RecordData数据列表的json array字符串 |
| `complete` | `() => void` | 否 | - | - | 接口调用结束的回调函数（调用成功、失败都会执行） |
| `success` | `(params: boolean) => void` | 否 | - | - | 接口调用成功的回调函数 |
| `fail` | `(params: Object) => void` ↓见下方 | 否 | - | - | 接口调用失败的回调函数 |

#### fail 回调参数

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `errorMsg` | `string` | 错误信息 |
| `errorCode` | `string \| number` | 错误码 |
| `innerError` | `Object` | 错误扩展 |

##### fail(params).innerError 的属性

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `errorCode` | `string \| number` | 错误扩展码 |
| `errorMsg` | `string` | 错误扩展信息 |


### 错误码

| 错误码 | 错误信息 |
| --- | --- |
| `-1001` | 手机版本不支持 |
| `-1002` | 数据转换异常 |
| `-1003` | 数据为空 |
| `-1004` | 提交 Health Connect 失败 |
| `-1005` | 服务异常，请检查 Health Connect 是否已开启自启动且电源管理无限制 |

### 示例代码

#### 基础调用

```tsx
import { health } from '@ray-js/ray'

const { insertRecords } = health;

const records = JSON.stringify([{ type: 'steps', value: 1000 }]);
insertRecords({
  request: records,
  success: (result) => {
    console.log('insertRecords success', result);
  },
  fail: (error) => {
    console.log('insertRecords fail', error.errorMsg);
  },
});
```
