- I have a class that uses class methods to call an API. The calls to the API return a Promise object.
- When I call the class method on an instance of the class, and use .then, if my arrow function in the class method declaration points to a { }, I get a TypeError: Undefined error
- This does not happen if my arrow function points directly to the API function I'm calling (just omitting the {} for the return block fixes the bug? Why?)
- Is there some kind of variable scoping issue going on here that I'm not aware of?
The function that I call on the class instance:
bar.doCreateUserWithEmailAndPassword.then(auth => {console.log(auth)}
WORKS AS EXPECTED:
class Foo extends Component{
constructor() {this.auth = firebase.app.auth}
...
doCreateUserWithEmailAndPassword = (email, password) =>
this.auth.createUserWithEmailAndPassword(email, password)
TYPE ERROR:UNDEFINED:
class Foo extends Component{
constructor() {this.auth = firebase.app.auth}
...
doCreateUserWithEmailAndPassword = (email, password) => {
this.auth.createUserWithEmailAndPassword(email, password)
}