0

Why doesn't the debugger line get hit? Please let me know how to modify the code so it does get hit.

async _store_token(tok){  // tried this line with and without 'async', just to see
      debugger;     // THIS LINE DOESN'T GET CAUGHT BY DEBUGGER
  }

  componentWillMount(){

     //debugger;

     axios({
  method: 'post',
  url: BASE_URL + '/users/login',
  data: {
      email: "email1@mail.com",
    password: "passpass"
  }
})
  .then(async function (res) {
    debugger;                       // CODE IS FINE AT THIS DEBUG STATEMENT
    _store_token(res.token);  // TRIED TO USE 'this._store_token(res.token);'
                               // but it didn't work either



})
mbhemming
  • 67
  • 1
  • 7

1 Answers1

0

The reason is scope of this keyword. May be you want to use arrow method functionality.

example

then(async res => {})
mooga
  • 3,136
  • 4
  • 23
  • 38
Garry
  • 536
  • 1
  • 5
  • 11
  • thanks for the reply. I tired it with the 'this' keyword and without it. Both didn't work – mbhemming May 16 '19 at 20:35
  • it worked with both the 'this' keywords and the arrow function. How would I get it to work if i wasn't using the arrow function? – mbhemming May 16 '19 at 20:37
  • You need to pass 'this' to any variable on function level scope and then you can use it inside anonymous function without using arrow method. like `componentWillMount() { //debugger; const self = this; axios({ method: "POST", url: BASE_URL + '/users/login', }).then(async function(res) { self._store_token().bind(); }); }` – Garry May 17 '19 at 17:25
  • Also I would suggest use `componentDidMount` the `componentWillMount`. Here is the one link I found quickly https://stackoverflow.com/questions/46043832/why-componentwillmount-should-not-be-used – Garry May 17 '19 at 17:28