writeWeight(record)

Writes a body-weight record to the device’s health store. If needed, presents a permissions prompt to the user to request access to health data first, and then writes the record.

Syntax 

1import { getHealthService } from 'lightning/mobileCapabilities';
2writeWeight(record: WeightRecord): Promise<void>

Parameters 

  • record—(Required) A WeightRecord object that represents the weight measurement to write. Provide a value greater than 0 and a valid unit.

Returns 

  • A resolved promise returns no object.

  • A rejected promise returns a HealthServiceFailure object containing details about the error.

Usage 

This function writes a single weight record to the device’s health store.

1const record = {
2  value: 80.5,
3  unit: 'kg',
4  startTime: Date.now(),
5  endTime: null
6};
7myHealthService
8  .writeWeight(record)
9  .then(() => {
10    console.log("Weight record saved.");
11  })
12  .catch((error) => {
13    console.error("Error code: " + error.code);
14    console.error("Error message: " + error.message);
15  });
  • When the record is written, the promise resolves with no value. Handle success in a then clause.
  • If an error occurs, the error is returned via a rejected promise. Handle errors in a catch clause.
  • If the user denies permission to write health data, the promise is rejected with a HealthServiceFailure.code value of USER_DENIED_PERMISSION.
  • Permission to write health data is granted separately from permission to read it.

See Also