【问题标题】:Infinite digest loop errors with Angular-Meteor, PublishComposite and Collection-HelpersAngular-Meteor、PublishComposite 和 Collection-Helpers 的无限摘要循环错误
【发布时间】:2015-11-09 04:35:19
【问题描述】:

我正在尝试借助这两个包在 angular-meteor 中获取一些相关数据:

流星收集助手:https://github.com/dburles/meteor-collection-helpers 流星发布复合:https://github.com/englue/meteor-publish-composite

我的设置如下所示:

模型/groups.js

Groups = new Mongo.Collection("groups");
Groups.helpers({
    usersByScore: function () {
        return Meteor.users.find({_id: {$in: this.users}}, {sort: {score: -1}});
    }
});
Groups.allow({
    insert: function (userId, group) {
        return Roles.userIsInRole(userId, 'admin');
    },
    update: function (userId, group, fields, modifier) {
        return Roles.userIsInRole(userId, 'admin');
    },
    remove: function (userId, group) {
        return Roles.userIsInRole(userId, 'admin');
    }
});

服务器/groups.js

Meteor.publishComposite("groupsWithUsers", function () {
    return {
        find: function() {
            if (Roles.userIsInRole(this.userId, 'admin')) {
                return Groups.find({});
            } else {
                return Groups.find({users: { $elemMatch: { $eq: this.userId}}});
            }
        },
        children: [
            {
                find: function(group) {
                    return Meteor.users.find({_id: {$in: (group.users) ? group.users : []}});
                }
            }
        ]
    };
});

client/matches/controller/scores.ng.js

angular.module('tippo').controller('ScoresController', ['$scope',
    function($scope){
        $scope.groups = Groups.find().fetch();
    }
]);

client/matches/views/scores.ng.html

<div ng-controller="ScoresController">
    <md-content class="md-padding">
        <md-tabs md-dynamic-height md-border-bottom>
            <md-tab label="{{ group.name }}" ng-repeat="group in groups">
                <md-content class="md-padding">
                    <md-list>
                        <md-list-item class="md-3-line" ng-repeat="user in group.usersByScore()">
                            <span>({{ user.score }})</span>
                        </md-list-item>
                        <md-divider></md-divider>
                    </md-list>
                </md-content>
            </md-tab>
        </md-tabs>
    </md-content>
</div>

客户端/routes.js

angular.module('tippo').config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
    function($urlRouterProvider, $stateProvider, $locationProvider){
        $locationProvider.html5Mode(true);

        $stateProvider.state('scores', {
            url: '/scores',
            templateUrl: 'client/matches/views/scores.ng.html',
            controller: 'ScoresController',
            resolve: {
                'subscribe': ['$meteor', function($meteor) {
                    return $meteor.subscribe('groupsWithUsers');
                }],
                "currentUser": ["$meteor", function($meteor){
                    return $meteor.requireUser();
                }]
            }
        });
    }
]);

发布/订阅部分工作正常,我可以通过控制台获取所有组和相关用户,但是一旦我添加“group.usersByScore() 中的用户”,我就会收到很多这样的错误:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"fn: regularInterceptedExpression","newVal":29,"oldVal":26}],[{"msg":"fn: regularInterceptedExpression","newVal":32,"oldVal":29}],[{"msg":"fn: regularInterceptedExpression","newVal":35,"oldVal":32}],[{"msg":"fn: regularInterceptedExpression","newVal":38,"oldVal":35}],[{"msg":"fn: regularInterceptedExpression","newVal":41,"oldVal":38}]]
http://errors.angularjs.org/1.4.2/$rootScope/infdig?p0=10&p1=%5B%5B%7B%22ms…rInterceptedExpression%22%2C%22newVal%22%3A41%2C%22oldVal%22%3A38%7D%5D%5D
at REGEX_STRING_REGEXP (angular.js:68)
at Scope.$get.Scope.$digest (angular.js:15723)
at Scope.$get.Scope.$apply (angular.js:15953)
at safeApply (angular-meteor-meteorCollection.js:154)
at Object.angularMeteorCollections.factory.AngularMeteorCollection.updateCursor.self.observeHandle.cursor.observe.addedAt (angular-meteor-meteorCollection.js:174)
at LocalCollection._observeFromObserveChanges.observeChangesCallbacks.addedBefore (observe.js:96)
at Object.LocalCollection._CachingChangeObserver.self.applyChange.addedBefore (observe.js:33)
at minimongo.js:373
at _.extend.runTask (fiber_stubs_client.js:42)
at _.extend.flush (fiber_stubs_client.js:70)

Exception in queued task: Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"fn: regularInterceptedExpression","newVal":29,"oldVal":26}],[{"msg":"fn: regularInterceptedExpression","newVal":32,"oldVal":29}],[{"msg":"fn: regularInterceptedExpression","newVal":35,"oldVal":32}],[{"msg":"fn: regularInterceptedExpression","newVal":38,"oldVal":35}],[{"msg":"fn: regularInterceptedExpression","newVal":41,"oldVal":38}]]
http://errors.angularjs.org/1.4.2/$rootScope/infdig?p0=10&p1=%5B%5B%7B%22ms…rInterceptedExpression%22%2C%22newVal%22%3A41%2C%22oldVal%22%3A38%7D%5D%5D
at REGEX_STRING_REGEXP (http://localhost:3000/packages/angular_angular.js?c3a778493846e58a63e652cd68b47914889a794c:98:12)
at Scope.$get.Scope.$digest (http://localhost:3000/packages/angular_angular.js?c3a778493846e58a63e652cd68b47914889a794c:15753:19)
at Scope.$get.Scope.$apply (http://localhost:3000/packages/angular_angular.js?c3a778493846e58a63e652cd68b47914889a794c:15983:24)
at safeApply (http://localhost:3000/packages/urigo_angular.js?3cfbb5ec69330c7589d9a07bff661ca2dd28d04d:1173:47)
at Object.angularMeteorCollections.factory.AngularMeteorCollection.updateCursor.self.observeHandle.cursor.observe.addedAt (http://localhost:3000/packages/urigo_angular.js?3cfbb5ec69330c7589d9a07bff661ca2dd28d04d:1193:11)
at LocalCollection._observeFromObserveChanges.observeChangesCallbacks.addedBefore (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:3862:28)
at Object.LocalCollection._CachingChangeObserver.self.applyChange.addedBefore (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:3799:56)
at http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:415:13
at _.extend.runTask (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:693:11)
at _.extend.flush (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:721:10)

任何帮助表示赞赏。

【问题讨论】:

    标签: javascript angularjs meteor


    【解决方案1】:

    它发生的原因是因为usersByScore() 每次调用函数时都会返回一个新的对象实例。因此摘要循环认为对象在不断更新,并放弃以避免卡住。

    目前遇到同样的问题,所以我不知道如何解决它(还)。

    编辑:在对这个问题进行了相当广泛的研究之后,我不得不得出结论,如果不对 angular-meteor 进行重大更改,就无法完成。

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。在 ng-repeat 循环中调用集合文档的集合助手会导致摘要循环。

      在我的情况下,我收集了多个团队中的每个人(个人文档上的团队 ID 数组)。

      在对问题进行调查后,我得出了与@mpkorstanje 类似的结论。最后,我发现自己在客户端应用程序中实现了一个 angular-meteor 助手,并在这种情况下放弃了收集助手。

      我的 angular-meteor 助手实现如下所示:

      // persistent instance cache
      this.teamsMap = [];
      
      this.helpers({
          teams() {
              const members = People.find({}).fetch();
      
              members.forEach(member => {
                  this.teamsMap[member._id] = member.teams.map(team => Teams.findOne(team));
              });
          }
      });
      

      还有一个控制器函数来访问视图中人员的团队:

      getTeams(member) {
          return this.teamsMap[member._id];
      }
      

      使用此实现,反应性和视图绑定按需要工作,我没有得到摘要循环。不幸的是,它有一种hacky的味道。希望它可以帮助某人作为一种解决方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多