OvalMoney / react-native-fitness

A React Native module to interact with Apple Healthkit and Google Fit.
MIT License
344 stars 68 forks source link

Can't get any results... #22

Closed VictorNIvanov closed 4 years ago

VictorNIvanov commented 4 years ago

Hello,

I cant seem to get any results returned from either Fitness.getSteps or Fitness.getDistance. I have tested this on a simulator with manually entered data in the HealthKit as well as on a device that has walking distance and steps data for each day... (iOS)

The code that I am using is the following:

let theDates = { startDate: "2019-12-02", endDate: "2019-12-03" };

let steps = Fitness.getSteps(theDates);

console.log("Steps...");
console.log(steps);

All it returns for any date or combination of dates is :

Steps...
{"_40": 0, "_55": null, "_65": 0, "_72": null}

I have double checked and the application does have access to the HealthKit data. Any help will be greatly appreciated!!

Have an awesome day!

Francesco-Voto commented 4 years ago

Hello @VictorNIvanov, can be possibile you are not using a Promise? it should be

let theDates = { startDate: "2019-12-02", endDate: "2019-12-03" };

let steps = await Fitness.getSteps(theDates);

console.log("Steps...");
console.log(steps);

Please let me know if it helps.

VictorNIvanov commented 4 years ago

Hello @Francesco-Voto,

Thank you for the quick response! Adding "await" did work... just had to move it to an async function.

I will leave a working example just in case anybody else has difficulties with this...

async getSteps(){

    let theDates = { startDate: "2019-12-02", endDate: "2019-12-04" };

    let steps = await Fitness.getSteps(theDates);

    console.log("Steps...");
    console.log(steps);

    if(steps[0].quantity){
        this.setState({ steps: steps[0].quantity }); // update the app state to refresh the UI
    }

}