getWeight(options)

Reads body-weight records from the device’s health store. If needed, presents a permissions prompt to the user to request access to health data first, and then returns the records that match the options you provide.

Syntax 

1import { getHealthService } from 'lightning/mobileCapabilities';
2getWeight(options: GetWeightOptions|null): Promise<Array>

Parameters 

  • options—A GetWeightOptions object to filter the records by time range, limit the number of records, and set the unit for the returned values. Pass null to use the defaults.

Returns 

  • A Promise object that resolves as an array of WeightRecord objects. If no records match, the array is empty.

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

Usage 

This function reads weight records from the device’s health store and returns them as an array.

1const options = {
2  startTime: null,
3  endTime: null,
4  limit: 10,
5  preferredUnit: 'kg'
6};
7myHealthService
8  .getWeight(options)
9  .then((results) => {
10    this.weightRecords = results;
11  })
12  .catch((error) => {
13    this.weightRecords = [];
14    console.error("Error code: " + error.code);
15    console.error("Error message: " + error.message);
16  });
  • When records are read, the result is returned via a fulfilled promise. Handle successful results 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 read health data, the promise is rejected with a HealthServiceFailure.code value of USER_DENIED_PERMISSION.
  • The device’s health store records weight without a unit. HealthService converts the returned values to the unit that you set in preferredUnit. If you don’t set a unit, HealthService returns values in kilograms.

See Also