question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'replace' of undefined when clicking browser back // production only bug

See original GitHub issue

Hey guys, I think this is the right place to post this, please correct me if I’m wrong. I recently deployed a meteor project to this url

After loading the root url and programmatically routing to another path (with FlowRouter.go), then clicking the browser’s back button this error pops up.

Uncaught TypeError: Cannot read property 'replace' of undefined

I deployed the project with the --debug flag and the error occurs in the _.each block with path.replace on line 435…

Router.prototype.initialize = function () {
    function initialize() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      if (this._initialized) {
        throw new Error('FlowRouter is already initialized');
      }

      var self = this;

      this._updateCallbacks(); // Implementing idempotent routing
      // by overriding page.js`s "show" method.
      // Why?
      // It is impossible to bypass exit triggers,
      // because they execute before the handler and
      // can not know what the next path is, inside exit trigger.
      //
      // we need override both show, replace to make this work
      // since we use redirect when we are talking about withReplaceState                                              

      _.each(['show', 'replace'], function (fnName) {
        var original = self._page[fnName];

        self._page[fnName] = function (path, state, dispatch, push) {
          if (!self.env.reload.get() && self._current.path === path) {
            return;
          }

          `path = path.replace(/\/\/+/g, '/');    // 435`
          original.call(this, path, state, dispatch, push);
        };
      }); // this is very ugly part of pagejs and it does decoding few times
      // in unpredicatable manner. See #168      
      // this is the default behaviour and we need keep it like that
      // we are doing a hack. see .path()

      this._page.base(this._basePath);

      this._page({
        decodeURLComponents: true,
        hashbang: !!options.hashbang
      });

      this._initialized = true;
    }

    return initialize;
  }();

When the back button is clicked, the route path (in the url bar) changes to the correct path, but the corresponding template does not render.

This bug can be reproduced by going to this url, clicking “save me money”, then clicking the browser back button.

On the shop insurance template there’s an X with a click event including FlowRouter.go('/') Weirdly enough, after clicking that X when the error is thrown, the root url template loads and then the error won’t happen again until you refresh the browser.

Note: everything works as intended locally, this only happens in production

Any help would be much appreciated, and if you need any more details please let me know!

Thanks, Dylan

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
dr-dimitrucommented, Nov 27, 2017

Hey @dylanmcgowan , I’m glad to help 😃 This is the long one ))

This issue took time to poke around. Thanks to community, Open Source, and @derwaldgeist . Do you ever imagine how this could be solved under close environments. Damn! I love Open Source (IMHO it should be always be written starting with capital letters)!

Feel free to close it once you will successfully update package, and your issue will be gone.

2reactions
dylanmcgowancommented, Nov 27, 2017

Yes I am using that okgrow:analytics package! Thank goodness I was loosing my mind over here. Thanks so much @dr-dimitru you’ve been such a massive help 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
React TypeError: Cannot read property 'replace' of undefined
1. The error tells you that imgUrl is undefined before you call replace() on it. · Could you please add the string value...
Read more >
[SOLVED] Cannot Read Property 'replace' of Undefined in JS
To fix the “cannot read property 'replace' of undefined” error, perform an undefined check on the variable before calling the replace() method ......
Read more >
Production only bug when clicking "back" button on certain ...
Uncaught TypeError : Cannot read property 'replace' of undefined. What's undefined is the route path. Before going to the target route the ...
Read more >
Avoiding those dang cannot read property of undefined errors
Uncaught TypeError : Cannot read property 'foo' of undefined. ​ The dreaded error we all hit at some point in JavaScript development.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found