1

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

//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 中,我得到了已登录的用户和具有 userId 的所需用户信息,我期待只有所需用户 ID 的用户信息,这是为什么呢?2.订阅“getUserInfo”不是反应性的,甚至在处理几块代码之后订阅也会丢失,然后在 askLikeController.$root.usersProfile 我只剩下登录用户的用户配置文件,我的猜测是我的订阅正在被用户的内置 Meteor 订阅所取代。

我该如何解决这些问题?

问候,赤丹

4

1 回答 1

0

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

> meteor remove autopublish

要在 angular-meteor 中获得反应性,您需要$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')})

}, 错误的);

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

askLikeController.$root.usersProfile = $scope.$meteorObject(Meteor.Users, {_id: $scope.getReactively('reqId')});
于 2015-10-20T16:24:50.123 回答