react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

[🐛] Poor error message when setBackgroundMessageHandler is not async

Open ToyboxZach opened this issue 3 years ago • 5 comments
trafficstars

Issue

I was getting a "TypeError: Cannot read property 'then' of undefined, js engine: hermes" on android

and could not figure out what was causing it


function onBackgroundNoticiation(){
}
messaging().setBackgroundMessageHandler(onBackgroundNoticiation);

Just discoverd that setBackgroundMessageHandler expects an async function so resolving that

async function onBackgroundNoticiation(){
}
messaging().setBackgroundMessageHandler(onBackgroundNoticiation);

Gets rid of that problem


Javascript

Click To Expand

package.json:

    "@react-native-firebase/app": "^14.11.1",
    "@react-native-firebase/crashlytics": "^14.11.1",
    "@react-native-firebase/messaging": "^14.11.1",

firebase.json for react-native-firebase v6:

# N/A

iOS

N/A

Android

Click To Expand

Have you converted to AndroidX?

  • [x ] my application is an AndroidX application?
  • [ ] I am using android/gradle.settings jetifier=true for Android compatibility?
  • [ ] I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->

Environment

Click To Expand

RN: 0.69.1

  • Platform that you're experiencing the issue on:
    • [ ] iOS
    • [ x] Android
    • [ ] iOS but have not tested behavior on Android
    • [ ] Android but have not tested behavior on iOS
    • [ ] Both
  • react-native-firebase version you're using that has this issue:
    • 14.11.1
  • Firebase module(s) you're using that has the issue:
    • messaging
  • Are you using TypeScript?
    • N

ToyboxZach avatar Aug 14 '22 17:08 ToyboxZach

Hi there! How do you propose to solve the issue? In my experience this is what typescript is for, really. You would receive a type error for attempting to send in a function with the wrong signature. We publish typescript types. What would you propose ?

mikehardy avatar Aug 14 '22 18:08 mikehardy

Maybe be able to catch the error and give a reasonable error message?

I'm not sure how hard that would be, when I looked deeper in the docs it was easy to tell the issue, but I had the error randomly returning for months and could not figure out because it was not descriptive.

It also seems non-intuitive that this would cause a crash in the first place, I don't see why it has to be asynchronous especially since its not returning anything anyways.

ToyboxZach avatar Aug 14 '22 18:08 ToyboxZach

android headless mode has a number of eccentricities and I assume (though do not know for certain, sorry) that it is related to them

I was thinking more about examining available methods on the Object prototype to see if then was a method, that is, reflectively inspect the passed in object.

That way you've got fail fast (when the method is called, so you see it in development) instead of fail-late where you get a crash and have to pick up the pieces

Something like this https://stackoverflow.com/questions/38508420/how-to-know-if-a-function-is-async

mikehardy avatar Aug 14 '22 19:08 mikehardy

I just took a look at the code and I see that it is just getting passed back in to react native.

I do believe this would resolve the problem completely

AppRegistry.registerHeadlessTask("ReactNativeFirebaseMessagingHeadlessTask", () => {
  if (!backgroundMessageHandler) {
    // eslint-disable-next-line no-console
    console.warn(
      'No background message handler has been set. Set a handler via the "setBackgroundMessageHandler" method.'
    );
    return () => Promise.resolve();
  }
  return async (remoteMessage) => {
    return await backgroundMessageHandler(remoteMessage);
  };
});

But not sure if that is good standards are anything.

I do see it is my fault for just ignoring the types on this module out of laziness, so I could be fine with a no-fix here, and hopefully just the existence of this bug will help other people who are running into a similar problem.

ToyboxZach avatar Aug 14 '22 19:08 ToyboxZach

I think you are getting that error because the DataModel.getList() doesn't return a Promise so you can have access to Promise then chain. All you have to do is to add a return keyword in the getList before the fetch function so the getList method can return a resolve Promise instead of undefined

Mbiakop1 avatar Sep 07 '22 10:09 Mbiakop1

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar Dec 05 '22 19:12 github-actions[bot]