对用户集合的反应性订阅

时间:2015-10-20 07:23:09

标签: meteor angular-meteor

我正在尝试订阅与登录用户不同的用户的profdle信息,但我遇到的问题如下所述 我正在使用角度材料,我的代码如下所示:

//publish user info upon following user
Meteor.publish("getUserInfo", function (userId) {
    return (Meteor.users.find({_id: userId}, {fields: {profile: 1}}));
});

//subscribe
 $scope.$meteorSubscribe("getUserInfo", askLikeController.$root.askLike[0].userId).then(function (subscriptionHandle) {
            //Second element in the userProfile array will have the profile of required user
            askLikeController.$root.usersProfile = $meteor.collection(Meteor.users, false);   
        });

问题: 1.在变量askLikeController。$ root.usersProfile中,我得到了loggedIn用户和所需的用户信息userId,我期望userinfo只有所需的userId,为什么会这样? 2.订阅" getUserInfo"不是被动的,甚至在处理几个代码块然后在askLikeController中丢失了订阅。$ root.usersProfile我只留下登录用户的用户配置文件,我的猜测是我的订阅被内置的Meteor取代订阅用户。

如何解决问题?

此致 赤丹

1 个答案:

答案 0 :(得分:0)

首先,请确保已删除自动发布:

> meteor remove autopublish

要获得角度流星的反应性,您需要$meteor.autorun$scope.getReactively。这是一个例子:

// we need the requested id in a scope variable
// anytime the scope var changes, $scope.getReactively will
// ... react!
$scope.reqId = askLikeController.$root.askLike[0].userId;

$meteor.autorun($scope, function() {
    $scope.$meteorSubscribe('getUserInfo', $scope.getReactively('reqId')));
}).then(function(){
    askLikeController.$root.usersProfile = $meteor.collection(Meteor.users, false);   
})

仅获取您选择的用户:注意 - 登录的用户始终已发布。因此,您需要在客户端指定要查看的用户,就像在发布方法上所做的那样。所以,在订阅方法中:

askLikeController.$root.usersProfile = $meteor.collection(function() {
    return Meteor.Users.find({_id: $scope.getReactively('reqId')})

},false);

此时,最好将其更改为对象而不是集合:

askLikeController.$root.usersProfile = $scope.$meteorObject(Meteor.Users, {_id: $scope.getReactively('reqId')});
相关问题