Meteor:异步函数回调异常:onAfterAction

Meteor:Exception in callback of async function: onAfterAction

本文关键字:异常 onAfterAction 回调 函数 异步 Meteor      更新时间:2023-09-26

闲置问题:我想用闲置代码(pages=集合名称)更新路由中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的$inc(增量)操作符:,就可以非常简化onAfterAction钩子

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