2015-10-20 55 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'發佈函數看起來像什麼? –