0

I have an ajax request that I pass a callback function to. When the ajax succeeds it successfully triggers the callback. But when I enter the function (which is nested in the route) and try to call this it errors saying that this is undefined.

I need to access the route object so I can redirect the user.

Here's what the route looks like:

export default Ember.Route.extend(UnauthenticatedRouteMixin, {
  session: Ember.inject.service('session'),
  user_service: Ember.inject.service('user'),
  actions: {
      var user = "user";
      var password = "password";
      var email = "email";
      this.get('user_service').logInUser(email, password, this.handleResponse, this.handleError);
  },
  handleResponse: function(request) {
      console.log(this);
  },
  handleError: function(error) {
      console.log(error);
  }
});
  • inside actions hash , you are declaring properties ... update ur question with exact code. – Ember Freak Oct 07 '16 at 02:50
  • and also include `logInUser` method code in `user` service too. – Ember Freak Oct 07 '16 at 02:57
  • everything i define here is listed. also, the ajax query is irrelevant. the callback is made successfully. the issue is that `this`—which represents the route—is `undefined` in the callback – user3661956 Oct 07 '16 at 03:42
  • This cannot be your actual code, the *variables* `handleResponse` and `handleError` are not defined. Are you sure you are not passing `this.handleResponse` to `logInUser`? Anyways, the duplicate will show various solutions. Especially see the section "Common problem: Using object methods as callbacks / event handlers". – Felix Kling Oct 07 '16 at 03:45

0 Answers0