lucaspbordignon / rn-apple-healthkit

A React Native package for interacting with Apple HealthKit
https://www.npmjs.com/package/react-native-health
MIT License
520 stars 298 forks source link

Unhandled JS Exception: _this._fetchStepCountData is not a function #27

Open ChaoTzuJung opened 6 years ago

ChaoTzuJung commented 6 years ago

I follow the setting and code with rn-apple-healthkit's document

However I got a strange mistake !!

Anyone have the answer?

`import React, { Component } from 'react'; import { ScrollView, Navigator, View, NativeAppEventEmitter, } from 'react-native'; import axios from 'axios'; import AppleHealthKit from 'rn-apple-healthkit';

import AlbumDetail from './AlbumDetail';

const d = new Date();

const PERMS = AppleHealthKit.Constants.Permissions;

const HKOPTIONS = { permissions: { read: [ PERMS.StepCount, PERMS.Height, PERMS.Weight, PERMS.DateOfBirth, PERMS.BodyMassIndex, ], write: [ PERMS.StepCount, PERMS.Weight, PERMS.BodyMassIndex, ], }, date: d.toISOString() };

AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => { if (err) { console.log('HealthkitInitError: ', err); return; } console.log('HealthkitInitSucess');

AppleHealthKit.initStepCountObserver({}, () => {});

this.sub = NativeAppEventEmitter.addListener( 'change:steps', (event) => { // mistake is here!!! this._fetchStepCountData(); console.log(event); } ); });

// StepCounter Example AppleHealthKit.getStepCount(HKOPTIONS, (error, results) => { if (error) { console.log('getStepCountError: ', error); return; } console.log(results); });

class albumList extends Component { state = { albums: [] };

componentWillMount() { axios.get('https://rallycoding.herokuapp.com/api/music_albums') .then(response => this.setState({ albums: response.data })) .then(response => console.log(response)); }

componentDidMount() { console.log(this.state.albums); }

// when the component where the listener was added unmounts, call the 'remove' method of the subscription object. componentWillUnmount() { this.sub.remove(); };

renderAlbums() { return this.state.albums.map(album =>

);

} render() { return (

{this.renderAlbums()}
);

} }

export default albumList;`

Aishwarya-Surana commented 6 years ago

Were you able to correct it? @ChaoTzuJung

ChaoTzuJung commented 6 years ago

Actually , I still not solve this problem. I just copy code from document, hovever there were something wrong I don't know why I got this error~~

Aishwarya-Surana commented 6 years ago

How did you setup HKObserverQuery, as said in the documentation?

ChaoTzuJung commented 6 years ago

I just setup initHealthKit sucessfully, and my mobile screen ask me to approve connect my app and healthkit~~ And then another feature such as step count ... I add this code from documentation to my project , it doesn't work !! So I hope someone can share their code which is sucess to run rn-apple-healthkit

arzola commented 4 years ago

You should implement that function with something like

_fetchStepCountData() {

const today = moment()

const options = {
      startDate: today.toISOString(true)
    }

    AppleHealthKit.getStepCount({
      date: options.startDate
    }, (err, results) => {
      if (err) {
        console.log(err)
        return
      }

      const steps = (typeof results === 'object')
        ? Math.floor(results.value)
        : 0

      if (steps > 0) {
        this.setState({
          pastStepCount: steps
        })
      }

    })
}