Subcribe获取的记录多于服务器发布的记录

时间:2015-10-10 13:51:38

标签: meteor angular-meteor

我正在尝试从服务器发布所有管理员用户

服务器上的

是这样的:

Meteor.publish("users_with_roles", function (options, role) {
    //{fields: {emails: 1, profile: 1}}
    Counts.publish(this, 'numberOfUsers', Meteor.users.find({$and:[
            {'roles.tripro': {$exists: true}},
            {'roles.tripro': role}
        ]}),
        { noReady: true });

    return Meteor.users.find({
            $and:[
                    {'roles.tripro': {$exists: true}},
                    {'roles.tripro': role}
                 ]
        }, options);
});

然后在客户端,我正在尝试订阅:

$meteor.autorun($scope, function() {
        $meteor.subscribe('users_with_roles', {
            limit: parseInt($scope.getReactively('perPage')),
            skip: (parseInt($scope.getReactively('page')) - 1) * parseInt($scope.getReactively('perPage')),
            sort: $scope.getReactively('sort'),
            fields: {emails: 1, profile: 1}
         },'admin').then(function() {
                $scope.usersCount = $meteor.object(Counts ,'numberOfUsers', false);
                console.log('user counter:' + $scope.usersCount.count);
                $scope.users.forEach( function (user) {
                    //    user.onClicked = function () {
                    //        //$state.go('userProfile', {userId: user._id});
                    //    };
                    console.log(user._id);
                });
            },
            function(error)
            {
                console.log(error);
            }
        );
    });


    $scope.users =  $meteor.collection(function() {
        console.log('looking for role: ' + role);
        return Meteor.users.find({}, {
            //sort : $scope.getReactively('sort')
        });
    });

但是,从日志记录开始,客户端似乎收到了所有用户,但从服务器端登录,它确实给出了正确的结果。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

这里要考虑几件事。

  1. 当您请求用户时,您将始终拥有“您”。因此,如果您登录的用户不是管理员,它仍会显示在集合中。

  2. 因为您使用的是$meteor.subscribe而不是$scope.$meteorSubscribe,所以当范围被销毁时您没有清除订阅,因此它可能与其他范围的客户端上的其他订阅混合在一起。