user1072337
user1072337

Reputation: 12945

Exception in callback of async function: TypeError: window.scrollTo is not a function (Meteor application)

I am trying to run a Meteor.js application using scrollTo(0,0) to get each of my iron-routes to go to the top of the page:

client/lib/router.js:

    Router.route('/services', {
        name: 'services',
        template: 'services',
      onAfterAction: function () {
        scrollTop();
      }  
    });

Router.route('/inquiry', function() {
    this.layout('inquiry');
    onAfterAction: function () {
      scrollTop();
  } 
});

    function scrollTop() {
        window.scrollTo(0, 0);
    }

Console error:

meteor.js:880 Exception in callback of async function: TypeError: window.scrollTo is not a function
    at scrollTop (http://localhost:3000/app/client/lib/router.js?942b5705d17b5d736fe545b9dd17f3ea42238776:45:12)
    at Router.route.onAfterAction (http://localhost:3000/app/client/lib/router.js?942b5705d17b5d736fe545b9dd17f3ea42238776:17:9)
    at RouteController.runHooks (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:265:7)
    at RouteController._runRoute (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:551:8)
    at Function.Route.dispatch (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:848:18)
    at route (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:705:11)
    at boundNext (http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:418:31)
    at http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:22
    at dispatch (http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:442:3)
    at http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:385:13

Upvotes: 0

Views: 1456

Answers (3)

user1072337
user1072337

Reputation: 12945

This error seemed to go away when I used .scroll(0,0) instead of .scrollTo(0,0). I am not sure why this is the case, but the error is gone and the functionality is there using scroll

Upvotes: 0

Thai Tran
Thai Tran

Reputation: 9935

Except the syntax bug like @Michel said, you might need to have

onAfterAction: function () {
    if (this.ready()) {
        scrollTop();
    }
}

to ensure that the template is rendered

Upvotes: 0

Michel Floyd
Michel Floyd

Reputation: 20226

Your second route is incorrect, you've mixed the functional style setup with the object style of i-r.

Router.route('/inquiry', function() {
    this.layout('inquiry');
    onAfterAction: function () {
      scrollTop();
  } 
});

Should be:

Router.route('/inquiry', {
  template: 'inquiry',
  onAfterAction: function () {
    scrollTop();
  }
});

Upvotes: 2

Related Questions