Fallowing Problem:I want to update a collection in the onAfterAction hook in a route with the fallowing code(pages = name of collection):
var document = pages.findOne({page: "stats"})._id;
console.log(document);
var counter = pages.findOne({_id: document}).counter + 1;
console.log(counter);
pages.update(document, { $set: { counter: counter },});
And here the whole Route:
Router.route('/stats',{
template: 'stats',
name: 'stats',
onBeforeAction: function(){
//Ladeproblem muss noch gelöst werden
var currentUser = Meteor.userId();
if(currentUser){
this.next();
} else {
Router.go('/');
}
},onAfterAction: function(){
var document = pages.findOne({page: "stats"})._id;
var counter = pages.findOne({_id: document}).counter + 1;
pages.update(document, { $set: { counter: counter },});
}
});
Does anybody know what is wrong or what i've to change because i'm getting this error:
Exception in callback of async function: onAfterAction@http://localhost:3000/app/app.js?hash=b9531ac754b452020ef7df959d83e227372272a7:647:28
RouteController.prototype.runHooks@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:267:5
RouteController.prototype._runRoute@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:553:3
Route.prototype.dispatch@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:850:10
Route/route@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:707:5
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:420:16
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:367:14
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:367:14
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:367:14
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:367:14
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:367:14
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:367:14
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?hash=d50ce351195109ce13c1bba2b7241c22082e8f23:444:3
RouteController.prototype.dispatch/</<@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:387:7
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:351:5
Tracker.Computation@http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:239:5
Tracker.autorun@http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:590:11
RouteController.prototype.dispatch/<@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:385:5
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:617:12
RouteController.prototype.dispatch@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:384:3
Router.prototype.dispatch@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:1694:3
onLocationChange@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:1778:20
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:351:5
Tracker.Computation@http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:239:5
Tracker.autorun@http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:590:11
Router.prototype.start@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:1771:31
Router/</<@http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:974:9
.withValue@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1077:17
withoutInvocation/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:464:26
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:17
onGlobalMessage@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:401:11
EventListener.handleEvent*usePostMessage@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:410:5
@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:439:3
@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:292:2
@http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:11:2
This is just a suspicion but if you are changing the document that the current route is displaying it's possible you have created an infinite loop - changing the document causes the route to be reactively re-rendered and then you increment the counter and then the route renders again, repeat.
Also your onAfterAction
hook could be quite simplified by just using MongoDB's $inc
(increment) operator:
onAfterAction: function(){
page.update({ page: "stats" },{ $inc: { counter: 1 }})
}