Skip to content

1.0: redirectTo does not let me proceed to the requested state #3117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cadilhac opened this issue Oct 29, 2016 · 3 comments
Closed

1.0: redirectTo does not let me proceed to the requested state #3117

cadilhac opened this issue Oct 29, 2016 · 3 comments
Milestone

Comments

@cadilhac
Copy link

cadilhac commented Oct 29, 2016

I checked the docs and see that redirectTo can contain a condition.

// a fn conditionally returning a {state, params}
.state('F', {
  redirectTo: (trans) => {
    if (trans.params().foo < 10)
      return { state: 'F', params: { foo: 10 } };
  }
})

In my own code, I check if the user is logged in. If he is, I redirect to another state. Otherwise, I just want to go to the requested state. The problem is that if the condition is not met an error occurs and we won't go in the state initially requested.

angular.js:13920 TypeError: Cannot read property 'state' of undefined
at handleResult (redirectTo.ts:25)
at processQueue (angular.js:16383)
at angular.js:16399
at Scope.$eval (angular.js:17682)
at Scope.$digest (angular.js:17495)
at Scope.$apply (angular.js:17790)
at angular.js:19621
at completeOutstandingRequest (angular.js:5964)
at angular.js:6243

As a workaround, I returned an empty object {} instead of nothing. It allows your code to go to the end without errors.

function handleResult(result: any) {
    let $state = trans.router.stateService;

    if (result instanceof TargetState) return result;
    if (isString(result)) return $state.target(<any> result, trans.params(), trans.options());
    if (result['state'] || result['params'])
      return $state.target(result['state'] || trans.to(), result['params'] || trans.params(), trans.options());
  }
@christopherthielen christopherthielen added this to the 1.0.0-beta.4 milestone Nov 1, 2016
@christopherthielen
Copy link
Contributor

Thanks, that does sound like a bug.

@christopherthielen
Copy link
Contributor

christopherthielen commented Nov 1, 2016

Think we simply need to exit early from this function when result is null or undefined

https://github.com/ui-router/core/blob/1c6220c3938818b0a97796713d1b033231458318/src/hooks/redirectTo.ts#L20-L27

@cadilhac
Copy link
Author

cadilhac commented Nov 1, 2016

Perfect. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants