陷阱问题:我想更新一个路径中的onAfterAction挂钩中的一个集合,其中包含以下代码(pages =收集名称):
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 },});
这里整个路线:
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 },});
}
});
有人知道出了什么问题,或者我有什么要改变的,因为我收到了这个错误:
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
答案 0 :(得分:0)
这只是一种怀疑,但是如果您要更改当前路径正在显示的文档,则可能创建了无限循环 - 更改文档会导致路径被反应性地重新呈现,然后您递增计数器然后路线再次渲染,重复。
只需使用MongoDB的onAfterAction
(递增)运算符,您的$inc
挂钩就可以非常简化:
onAfterAction: function(){
page.update({ page: "stats" },{ $inc: { counter: 1 }})
}