Skip to content

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

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
teha-at opened this issue Aug 20, 2021 · 1 comment · Fixed by #34
Closed

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

teha-at opened this issue Aug 20, 2021 · 1 comment · Fixed by #34

Comments

@teha-at
Copy link

teha-at commented Aug 20, 2021

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.

@NathanWalker
Copy link
Contributor

Published in 12.2.0 now.

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

Successfully merging a pull request may close this issue.

2 participants