react-native-batch-push icon indicating copy to clipboard operation
react-native-batch-push copied to clipboard

Lib throws an error on identifier set to `undefined`

Open jcalixte opened this issue 6 years ago • 1 comments

Hi!

When I want to unsubscribe a user to batch when he wants to logout, I try to set the identifier to undefined as typed by the library (the code calls the method clearUserId).

import { Batch, BatchUser } from "@bam.tech/react-native-batch";

class Notification {
  public constructor() {
    Batch.start();
  }

  public setUserId(id?: string): void {
    BatchUser.editor()
      .setIdentifier(id)
      .save();
  }

  public clearUserId(): void {
    this.setUserId();
  }
}

export const NotificationService = new Notification();

throws this error image

When I set the identifier to null (as the doc in Batch seems to define it), it works like a charm. But TypeScript is not happy : Argument of type 'null' is not assignable to parameter of type 'string | undefined'.

Does the .d.ts file need to be updated?

Thank you.

jcalixte avatar Feb 11 '20 11:02 jcalixte

Same issue on Android. It's due to ReadableNativeMap#getType that throw an NoSuchKeyException if value is undefined.

To fix this issue, this lib should check key existence (with hasKey) before using getType, or add a try/catch block on NoSuchKeyException and define as null in these case. Or disallow using undefined & allow null in TS declaration.

Aure77 avatar Mar 19 '20 17:03 Aure77