Skip to content

Router: Cannot read property 'path' of undefined #22643

Closed

Description

@cuiliang

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Optional param will not work if
(1) last component hast empty path,
(2) it's parent component has named path.

<a [routerLink]="[{param1: 'test'}]">This link will cause error</a>
image

Expected behavior

url parameter changed normally.

Minimal reproduction of the problem with instructions

https://angular-gjpvor.stackblitz.io/crisis-center/center
Open console to see errors.
What I have changed to the code from official example:

  1. change path of CrisisListComponent from '' to 'center':
const crisisCenterRoutes: Routes = [
  {
    path: '',
    component: CrisisCenterComponent,
    children: [
      {
        path: 'center',  <=== parent has named path
        component: CrisisListComponent,
        children: [
          {
            path: ':id',
            component: CrisisDetailComponent,
            canDeactivate: [CanDeactivateGuard],
            resolve: {
              crisis: CrisisDetailResolver
            }
          },
          {
            path: '',  <===  Empty path
            component: CrisisCenterHomeComponent
          }
        ]
      }
    ]
  }
];
  1. put routerLink in CrisisCenterHomeComponent
<div style='border: red 1px solid; padding:10px;'>
      <a [routerLink]="[{param1: 'test'}]">This link will cause error</a>
    </div>

or call router.navigate([{param: 'value'}], {relativeTo: this.route}) in component code will also cause error.

I have debugged and found some possible problem.
See this line

when error happens, startIndex is bigger than segmentGroup.segments.length, so p is undefined, p.path will be invalid.

Seems segmentGroup.segments is from url. For url /path/to/child, this array will contains 3 elements. If we have components with empty path, route levels will be bigger than that. startIndex will also bigger than that. Perhaps there is a bug here.

startIndex equals ActivatedRouteSnapshot._lastIndex of current component.

here is an example from out project:
image

Route definition of a sub module.

const routes: Routes = [
  {
    path: '',
    component: ProjectSettingsComponent,
    children: [
      {
        path: '',
        component: ProjectSettingsHomeComponent
      },
      {
        path: 'project-roles',
        component: ProjectRolesComponent
      },
      {
        path: 'workflow',
        component: WorkflowWrapperComponent,
        children: [
          {
            path: '',  //TODO: if empty, will cause error: ERROR TypeError: Cannot read property 'path' of undefined
            component: WorkflowComponent
          },
          {
            path: 'node/:workflowNodeId',
            component: WorkflowNodeConfigComponent
          }
        ]
      },
      {
        path: 'fields',
        component: ProjectFieldsComponent
      }
    ]
  }

];

What is the motivation / use case for changing the behavior?

Environment


Angular version: 5.2.7


Browser:
all

For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Activity

  1. added this to the needsTriage milestone on Mar 8, 2018
  2. cuiliang commented on Mar 12, 2018

    @cuiliang
    Author

    Hi @briancodes
    There are 3 type of params. What you mean is query parameters, but this is Optional Parameters.
    See this

    And this link will work if there is no empty path segment.

  3. briancodes commented on Mar 13, 2018

    @briancodes

    @cuiliang The [routerLink] input in a template cannot accept query parameters.
    <a [routerLink]="[{param1: 'test'}]"> will cause an error because it only accepts a string or an array of string paths. The directive does not accept parameters in the same fashion as this.router.navigate()

  4. cuiliang commented on Mar 13, 2018

    @cuiliang
    Author

    @briancodes It's not query paramemer, it's Optional Parameter.
    The [routerLink] array can take object item as optional parameters.
    You can see this answer on stackoverflow: https://stackoverflow.com/questions/44864303/sending-data-with-route-navigate-in-angular-2/44865817#44865817

  5. briancodes commented on Mar 13, 2018

    @briancodes

    @cuiliang My bad! Sorry, I see what you mean now

  6. jasonaden commented on May 3, 2018

    @jasonaden
    Contributor

    Closing as this appears resolved.

  7. teflonwaffles commented on May 8, 2018

    @teflonwaffles

    I'm facing problems related to this as well. Is there a specific pull request that fixed this? (I'd like a sense of what Angular version I need to wait for in order for this to be resolved.)

  8. jasonaden commented on May 9, 2018

    @jasonaden
    Contributor

    @teflonwaffles This was closed because it appeared resolved from the above conversation. To produce the result OP was looking for, you would change the code to:

    <a [routerLink] [queryParams]="{param1: 'test'}">This link will NOT cause error</a>

  9. reppners commented on Jun 1, 2018

    @reppners

    Just ran into this issue. It's not resolved. Optional matrix parameters will fail if empty routes are involved. Have to resort to query parameters to workaround it.

  10. uvv001 commented on Oct 18, 2018

    @uvv001

    Ran into this issue too. With route definition from first post can be reproduced by

    router.navigate([{key: "value"}], { relativeTo: activatedRoute })

    Workaround:

    urlTree = router.parseUrl(router.url);
    segments = urlTree.root.children.primary.segments;
    segment = segments[segments.length-1];
    Object.assign(segment.parameters, {key: 'value'});
    router.navigateByUrl(router.serializeUrl(urlTree));
  11. jcairney commented on Oct 18, 2018

    @jcairney

    @teflonwaffles This was closed because it appeared resolved from the above conversation. To produce the result OP was looking for, you would change the code to:

    <a [routerLink] [queryParams]="{param1: 'test'}">This link will NOT cause error</a>

    I'm sorry @jasonaden, though that may even be the solution I needed, nobody is going to figure that out without coming to this bug. On that grounds I'd say this should be reopened and given a proper fix.

  12. jcairney commented on Oct 18, 2018

    @jcairney

    Actually what I quoted above did not work for my situation.
    Related to what @reppners said, how do we change matrix parameters on the current route if empty routes are involved?

    I tried:
    <a [routerLink]="[{param1: 'test'}]">Still causes 'path' error</a>
    and
    <a [routerLink]="['.',{param1: 'test'}]">Also causes 'path' error</a>
    and
    <a [routerLink]="['../' + currentRequiredParamValueFromRouteParamsObservable,{param1: 'test'}]">Causes error when trying to navigate</a> Did this since the route to the empty path contained an :id required parameter in the last real segment that lead to the module with the empty path.

  13. devpreview commented on Dec 12, 2018

    @devpreview

    @jasonaden Same error.

    Route:

    const routes: Routes = [
      {
        path: '',
        component: IndexComponent
      },
      {
        path: 'parent',
        component: ParentComponent,
        children: [
          {
            path: 'child',
            component: ChildComponent,
            children: [
              {
                path: '',
                component: ThirdComponent
              }
            ]
          }
        ]
      }
    ];
    

    ThirdComponent link:

    <a [routerLink]="['.', {arg1: '1'}]">link</a>
    

    Error:

    ERROR TypeError: Cannot read property 'path' of undefined
    

    Reproduction: https://angular-router-error.stackblitz.io/parent/child
    Image of Reproduction

  14. devpreview commented on Dec 13, 2018

    @devpreview

    @jasonaden Is a any fix planned?

  15. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  16. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions