4

TL;DR - How can I delay the ProcessResult() function in aurelia-router.js from executing because I'm still waiting on the result of a promise in my code? It results in the right module rendering, with the wrong address/href.

Example: if you're on base-module, then click on admin, the admin module will load, but the href remains www.mycompany.com/#/base-module instead of www.mycompany.com/#/admin, and this error can be seen in the console:

ERROR [app-router] Error: Expected router pipeline to return a navigation result, but got [{}] instead

Longer version:

I have a prerender step in my router that checks whether the user is enabled for a particular module before rendering the view.

In my PreRenderStep class I have a run function, which calls a mediator to get the permissions for a user, then checks whether the module the user clicked on is in their enabled modules list. The call to the mediator involves a promise.

The problem is that the run promise in the prerender step resolves before a promise within the run method finishes. Therefore, the view is eventually rendered (because the user is enabled) but the previous href remains in the address bar.

Router:

 configureRouter(config, router) {
        config.title = "Tramonex";
        config.addPipelineStep('authorize', AuthorizeStep);
        config.addPreRenderStep(PreRenderStep);
        config.map([
            {
                route: ['', 'log-in-out'],
                name: 'home',
                moduleId: 'modules/authentication/log-in-out'
            },
            {
                route: 'passwordReset',
                moduleId: 'modules/authentication/password-reset',
            },
            {route: 'app', moduleId: 'app', auth: true},
            {
                route: 'base-module',
                name: 'base-module',
                moduleId: 'modules/base-module',
                href: 'base-module',
                nav: true,
                auth: true
            },
            {
                route: 'test1',
                name: 'test1',
                moduleId: 'modules/test1/test1',
                href: 'test1',
                nav: true,
                auth: true,
                settings: {moduleAuthRequired: true}

            },
            {
                route: 'test2',
                name: 'test2',
                moduleId: 'modules/test2/test2',
                href: 'test2',
                nav: true,
                auth: true,
                settings: {moduleAuthRequired: true}
            },
            {
                route: 'admin',
                name: 'admin',
                moduleId: 'modules/admin/admin',
                href: 'admin',
                nav: true,
                auth: true,
                settings: {moduleAuthRequired: true}
            },
        ]);

        this.router = router;
    }
}

PreRenderStep:

@inject(Mediator, AuthenticationService)
class PreRenderStep {

    constructor(mediator, authenticationService) {
        this.mediator = mediator;
        this.authenticationService = authenticationService;
    }

    run(navigationInstruction, next) {
       
        if (navigationInstruction.getAllInstructions().some(i => i.config.settings.moduleAuthRequired)) {

            this.redirect = false;
            this.mediator.getPermissionsForUser()
                .then(user => {
                    userPerms = user.modules;
                    var isEnabled = userPerms.includes(navigationInstruction.config.name);
                    if (!isEnabled) {
                        this.redirect = true;
                    }
                })
                .then(() => {
                    return this.redirect next.cancel(navigationInstruction.router.navigateToRoute('base-module')) : next();
                });
        }
        else {
            return next();
        }
    }
}

When the user clicks on a module that requires an auth check, the code is hit and whilst we're waiting on the promise from the mediator.getPermissionsForUser() to return, this code in aurelia-router.js is hit (the lines with stars):

function processResult(instruction, result, instructionCount, router) {
  if (!(result && 'completed' in result && 'output' in result)) {
    result = result || {};
    **result.output = new Error('Expected router pipeline to return a navigation result, but got [' + JSON.stringify(result) + '] instead.');**
  }

  var finalResult = null;
  if (isNavigationCommand(result.output)) {
    result.output.navigate(router);
  } else {
    finalResult = result;

    if (!result.completed) {
      if (result.output instanceof Error) {
        logger.error(result.output);
      }

      **restorePreviousLocation(router);**
    }
  }

5
  • 1
    Does it work if you return the promise from your prerender step: return this.mediator.getPermissionsForUser().then...?
    – mgiesa
    Dec 5, 2016 at 21:25
  • Also there is an authorize step that runs first that feels like the right place to do this check
    – mgiesa
    Dec 5, 2016 at 21:26
  • 1
    This answer suggests you are possibly just missing a return, ie return this.mediator.getPermissionsForUser().then(...) Dec 6, 2016 at 7:08
  • yessss, that's exactly what it was! Thanks guys, can't believe I missed that - doh! @mgiesa, if you want to post an answer I'll be happy to accept it as the answer (first dibs and all).
    – Naadof
    Dec 6, 2016 at 10:19
  • Nice! I'll add it as an answer
    – mgiesa
    Dec 6, 2016 at 13:21

1 Answer 1

3

You need to return the promise that you're creating in the run function.

return this.mediator.getPermissionsForUser()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.