2015-10-20 76 views
1

我正在尝试订阅不同用户的分配信息,而不是登录用户,但我面临的问题如下所述 我正在使用角度材料,我的代码如下所示:用户集合上的无效订阅

//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用户和所需的用户信息为用户id,我所期待的用户信息只有所需的用户id的,这是为什么? 2.订阅“getUserInfo”没有被动,甚至订阅在处理几块代码后丢失,然后在askLikeController中。$ root.usersProfile我剩下只有登录用户的用户配置文件,我的猜测是我的订阅正在被用户内置的流星订阅取代。

我该如何解决问题?

问候, 赤胆

回答

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')}); 
+0

嗨TJ,现在我能够得到想要的用户askLikeController $ root.usersProfile但这个变量没有反应,这是我的第二个问题仍然存在。一旦订阅,我要去数据库并更改订阅用户的用户配置文件,但是这在客户端订阅中没有得到更新?任何想法/线索? –

+0

嗨TJ,我想我找到了原因但没有回答,流星只有指向当前登录用户的光标,因此当我更改当前登录用户的配置文件时,配置文件也在客户端中更改。并且所需用户的光标仅在第一次订阅时运行。现在的问题是如何确保游标仍然指向“getUserInfo”订阅,而不是用户的默认订阅 –

+0

这就是发布函数getUserInfo应该做的事情。你的'getUserInfo'发布函数看起来像什么? –