令人讨厌的问题:我想使用以下代码更新路由中onAfterAction挂钩中的集合(页面=集合名称):

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

最佳答案

这只是一种怀疑,但是如果您要更改当前路径显示的文档,则可能创建了一个无限循环-更改文档会导致该路径被反应性地重新渲染,然后增加计数器,然后渲染路径再重复一次。

另外,只需使用MongoDB的onAfterAction(增量)运算符,就可以大大简化$inc钩子:

onAfterAction: function(){
  page.update({ page: "stats" },{ $inc: { counter: 1 }})
}

09-11 18:21