0

I'm getting the following error: Object is possibly 'null' on this.auth.currentUser how do I properly annotate this line to make the error go away?

Method:

doPasswordUpdate = (password: string): Promise<void> =>
    this.auth.currentUser.updatePassword(password)

Annotation: auth: firebase.auth.Auth

Any help is appreciatedQ!

Kyle
  • 149
  • 3
  • 10
  • Just google the error, there's a bunch of posts answering this, like this one: https://stackoverflow.com/questions/54884488/how-can-i-solve-the-error-ts2532-object-is-possibly-undefined – Jayce444 Oct 22 '19 at 02:17
  • Thank you for the reply. I tried a few solutions but most of them don't work like: doPasswordUpdate = (password: string): Promise | void => { if (this.auth) { this.auth.currentUser.updatePassword(password) } } or this.auth!.currentUser.updatePassword(password) it still throws the same error – Kyle Oct 22 '19 at 02:23
  • Well you'll have to check each step right, like `if (this.auth && this.auth.currentUser && this.auth.currentUser.updatePassword) { ... }`, or something similar, since any one of those could null. Might not need to check `this`, but I'm not sure. – Jayce444 Oct 22 '19 at 02:33
  • Thank you for the response! I see what I did wrong now ;) – Kyle Oct 22 '19 at 07:41

1 Answers1

2

In this case you are probably better off doing something like:

doPasswordUpdate = (password: string): Promise<void> =>
  this.auth.currentUser  
    ? this.auth.currentUser.updatePassword(password)
    : Promise.resolve()
Ahmad Ragab
  • 954
  • 1
  • 12
  • 24