Skip to content
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

change state inside $transition onStart hook function got Error #2977

Closed
xr opened this issue Sep 7, 2016 · 6 comments
Closed

change state inside $transition onStart hook function got Error #2977

xr opened this issue Sep 7, 2016 · 6 comments
Labels

Comments

@xr
Copy link

xr commented Sep 7, 2016

$transitions.onStart({
        to: (state) => state.auth
    }, () => {
        if (syncAuthCheckFailed()) {
                        $state.go('state_not_auth');
            return false;
        } else {
            return true;
        }
    });

error:

message: "The transition has been superseded by a different transition (see detail)."

without $state.go() everything is fine

update

even i just simply called the $state.go inside onEnter, same error appear.

onEnter: ['$state', function ($state) {
        $state.go('guest');
}]
@hulkish
Copy link

hulkish commented Sep 7, 2016

try this instead:

$transitions.onStart({
  to: (state) => state.auth
}, () => {
  if (syncAuthCheckFailed()) {
    return $state.target('state_not_auth');
  } else {
    return true;
  }
});

@xr
Copy link
Author

xr commented Sep 7, 2016

@hulkish thanks, but no use..

@christopherthielen
Copy link
Contributor

christopherthielen commented Sep 9, 2016

In ui-router legacy, people wondered why their transitions were failing silently. In 1.0, we added a default error handler which is printing the message that the original transition (to an auth state) was superseded by the new one (to guest). Read more here

  1. Disable the default error handler in production mode by calling $state.defaultErrorHandler(function() { /* do nothing */}); (or provide some meaningful filtering/error logging)

  2. The suggestion by @hulkish is the correct approach. Instead of forcibly superseding the original transition, use $state.target('guest') to nicely redirect the original transition.

Enable trace to get more insight into what is happening.

app.run(function($trace) { $trace.enable('TRANSITION'); })

@ashwaniare
Copy link

I followed the same steps but getting Error: Too many Transition redirects (20+) . I am using return $state.target('state_not_auth'); as you mentioned instead of state.go('state_not_auth')

@christopherthielen
Copy link
Contributor

@ashwaniare I suspect your hook is being run in an infinite loop.

  • Hook is invoked on initial transition
  • Hook redirects to state_not_auth
  • Hook is invoked on redirected transition
  • Hook redirects to state_not_auth
  • ...

Make sure your hook doesn't redirect if it's already heading to state_not_auth.

@0xAshish
Copy link

Solution
$transitions.onStart({
to: (state) => state.auth
}, () => {
if (syncAuthCheckFailed()) {
return $state.target('state_not_auth');

    } else {
        return true;
    }
});

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

No branches or pull requests

5 participants