【问题标题】:Meteor:Exception in callback of async function: onAfterActionMeteor:异步函数回调中的异常:onAfterAction
【发布时间】:2016-09-10 07:01:15
【问题描述】:

闲置问题:我想用闲置代码(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

【问题讨论】:

    标签: javascript html mongodb meteor meteor-blaze


    【解决方案1】:

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

    您的 onAfterAction 钩子也可以通过使用 MongoDB 的 $inc(增量)运算符来简化:

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

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 1970-01-01
      • 2014-12-01
      • 2015-05-07
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      相关资源
      最近更新 更多