-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warning: TypeError: _reactNativeHealth.default.initHealthKit is not a function (it is undefined) #399
Comments
Same problem im facing. im using native 0.76.3. version. so you solved this issue ? or the problem is with react-native-health dependency only. please let me know . |
@kapilw360 - I was able to fix it my looking at the file changes PR: You will need to make your index.js file in: look like the index.js in here. |
yah..i done. Thanxx man. |
@kapilw360 is correct. The issue is the Object.assign in the latest package index.js. You cannot object.assign native modules. This is a temporary fix with complete type support, to get the API back to the expected shape. import BrokenHealthKit, { HealthKitPermissions } from "react-native-health";
const NativeModules = require("react-native").NativeModules;
const AppleHealthKit = NativeModules.AppleHealthKit as typeof BrokenHealthKit;
AppleHealthKit.Constants = BrokenHealthKit.Constants; Then you can use as normal. For example: const permissions: HealthKitPermissions = {
permissions: {
read: [
AppleHealthKit.Constants.Permissions.HeartRate,
],
write: [],
},
};
AppleHealthKit.initHealthKit(permissions, (error) => {
if (error) {
console.log("[ERROR] Cannot grant permissions!", error);
return;
}
AppleHealthKit.getSleepSamples(
{
startDate: new Date(2020, 1, 1).toISOString(),
},
(error, results) => {
if (error) {
console.log("[ERROR] Cannot get sleep samples!", error);
return;
}
console.log("[SUCCESS] Sleep samples fetched!", results);
}
);
}); |
@warrenday: Thank you. This was EXTREMELY frustrating. |
Same problem here. Followed the fix in the PR and works. Please merge fast!! |
A workaround that doesn't require modifying the package is to assign the missing functions that you need. For example: import { NativeModules } from 'react-native';
import AppleHealthKit from 'react-native-health';
for(let key of ["initHealthKit"])
AppleHealthKit[key] = NativeModules.AppleHealthKit[key]; I don't know if this works for all of the interface since I only use a tiny part of it, but it got me up and running again. Since this package appears to be abandoned (no response to a showstopper bug since Nov), I poked around for alternatives. I see https://github.com/kingstinct/react-native-healthkit, which at least has commits in the last month. I haven't tried it yet, but I thought I'd mention it for anyone who wants to take a look. |
Thank you for this fix! I tried the other package and it seems like working very good! |
import React, { useEffect, useState } from "react";
import { View, Text, Button, Alert } from "react-native";
import AppleHealthKit, { HealthKitPermissions } from "react-native-health";
const App = () => {
const [isHealthKitAvailable, setIsHealthKitAvailable] = useState(false);
const [hasPermissions, setHasPermission] = useState(false);
const [steps, setSteps] = useState<number | null>(null);
const permissions: HealthKitPermissions = {
permissions: {
read: [AppleHealthKit.Constants.Permissions.Steps],
write: [],
},
};
const fetchSteps = () => {
const options = {
startDate: new Date(2024, 0, 1).toISOString(), // Example: fetch data from Jan 1, 2024
endDate: new Date().toISOString(),
};
};
useEffect(() => {
// Check if HealthKit is available
AppleHealthKit?.isAvailable((err, available) => {
if (err || !available) {
Alert.alert(
"HealthKit Not Available",
"This device does not support Apple HealthKit."
);
return;
}
setIsHealthKitAvailable(true);
}, []);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
{isHealthKitAvailable
?
Steps Count: ${steps ?? "Loading..."}
: "HealthKit not available"}
{isHealthKitAvailable && (
)}
);
};
export default App;
I dont know what am i missing here my library is installed properly ,pods are installed correctly but getting such error.
same error also there for isAvailable call.
Can anyone suggest or help me out to solve this issue?
The text was updated successfully, but these errors were encountered: