kingstinct/react-native-healthkit

requestAuthorization gives error on newly installed build

Closed this issue · 5 comments

I'm figuring this is a problem not everybody is having, just me missing a step or so.
I've just installed the package, updated app.json with infoPlist and entitlements.
After opening the app in a dev-build in my physical phone the app crashes immediately when requesting for authorization.

import HealthKit, { HKQuantityTypeIdentifier } from "@kingstinct/react-native-healthkit";

const authResult = await HealthKit.requestAuthorization([
	Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.walkingStepLength),
]);

console.log("authResult: ", authResult);

Image of error showing

Got same result when using the examples in readme of package

Hi @NiklasPainDrainer! I think the issue is that you need to wait for the authorization to be updated before calling useMostRecentQuantitySample (otherwise HealthKit will make your app crash by design). Try this:

import HealthKit, { HKQuantityTypeIdentifier } from "@kingstinct/react-native-healthkit";

const authResult = await HealthKit.requestAuthorization([
// here you should only specify the HKQuantityTypeIdentifier
	HKQuantityTypeIdentifier.walkingStepLength
]);

console.log("authResult: ", authResult);

// make sure this is called after the authResult is not shouldRequest anymore, for example in a conditionally rendered child component:
Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.walkingStepLength)

I see. The build is still green, and the build progress is completed and all after trying your code. Still same issue however.
The error shows for me even when trying the example code in the README. Is me who has set it up wrong perhaps?
Error saying Unhandled JS Exception: Unexpected identifier '_reactNativeHealthkit'. Expected ';' after variable declaration. no stack I thought by just printing out any data would confirm it's all set up properly but it feels like it isn't.

Are you sure your dev-build is up-to-date?

And just to make sure - the issue is the Unhandled JS Exception: Unexpected identifier '_reactNativeHealthkit'. Expected ';' after variable declaration. no stack and not that it's crashing - or is it both?

I noticed in the picture you posted earlier that you have 2 errors logged. It might be interesting to look at the first error as well - maybe it'll give more of a hint.

Managed to get it working. But I don't know why it behaved the way it did. I was reading the requestAuth at my Home screen, which is after a login function. But after changing it to trigger the requestAuth on a button press it was working. It was like React Native has not mounted yet, or something so everything crashed.

Glad you got it working! Let me know if there's anything else 🙂👍