Skip to content

Router: TypeError Cannot read property 'key' of undefined #29

Closed
@teha-at

Description

@teha-at

Environment

  • CLI: 8.0.2
  • Cross-platform modules:
  • Android Runtime: 8.0.0
  • iOS Runtime: 8.0.0
  • Plugin(s):
  • NativeScript-Angular: 12.0.6
  • Angular: 12.2.1

Describe the bug
Sometimes if I go back (this.routerExtensions.back();) within the router history the screen will be white (background color) with no elements because of an error appears and the rendering aborted:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'key' of undefined

The error throws at https://github.com/NativeScript/angular/blob/main/packages/angular/src/lib/legacy/router/ns-route-reuse-strategy.ts#L136:

const cache = this.cacheByOutlet[outletKey];
if (!cache) {
   return false;
}
// ...
const shouldAttach = isBack && cache.peek().key === key;

the cache.peek() returns an undefined and the following cache.peek().key fails.

Expected behavior
The peek() method shouldn't return an undefined even the cache is empty. In my opinion the result have to be an empty CacheItem (interface)

The second (worse) option is to check if the result of peek() is defined:

const shouldAttach = isBack && !!cache.peek() && cache.peek().key === key;

Additional context
I'm not sure but I think this happened because in my code I navigate to a page with clearHistory

this.router.navigate([...], {clearHistory: true})

and if a special flag is set directly after the first navigate a second will be fired without clearHistory. I don't no why the cache are empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions