rn-apple-healthkit
rn-apple-healthkit copied to clipboard
Unhandled JS Exception: _this._fetchStepCountData is not a function
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 => <AlbumDetail key={album.title} data={album} /> ); } render() { return ( <ScrollView> {this.renderAlbums()} </ScrollView> ); } }
export default albumList;`
Were you able to correct it? @ChaoTzuJung
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~~
How did you setup HKObserverQuery, as said in the documentation?
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
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
})
}
})
}