在我的社交应用程序(如FB)中,我有一个奇怪的需求,那就是在一次发布中合并同一个集合用户的两个游标!
Meteor服务器打印此错误:“发布函数返回集合用户的多个游标”。
也许在Meteor 0.7.2中做不到这一点,也许我搞错了。但是我已经看到了游标的结构非常简单,因为我可以进行一个简单的数组合并并返回一个游标?
客户端
Meteor.subscribe('friendById', friend._id, function() {
//here show my friend data and his friends
});
服务器
//shared functions in lib(NOT EDITABLE)
getUsersByIds = function(usersIds) {
return Meteor.users.find({_id: {$in: usersIds} },
{
fields: { // limited fields(FRIEND OF FRIEND)
username: 1,
avatar_url: 1
}
});
};
getFriendById = function(userId) {
return Meteor.users.find(userId,
{
fields: { // full fields(ONLY FOR FRIENDS)
username: 1,
avatar_url: 1,
online: 1,
favorites: 1,
follow: 1,
friends: 1
}
});
};
Meteor.publish('friendById', function(userId) { //publish user data and his friends
if(this.userId && userId)
{
var userCur = getFriendById(userId),
userFriends = userCur.fetch()[0].friends,
retCurs = [];
//every return friend data
retCurs.push( userCur );
//if user has friends! returns them but with limited fields:
if(userFriends.length > 0)
retCurs.push( getUsersByIds(userFriends) );
//FIXME ERROR "Publish function returned multiple cursors for collection users"
return retCurs; //return one or more cursor
}
else
this.ready();
});
发布于 2014-03-25 09:57:18
溶液
Meteor.publish('friendById', function(userId) {
if(this.userId && userId)
{
var userCur = getFriendById(userId), //user full fields
userData = userCur.fetch()[0],
isFriend = userData.friends.indexOf(this.userId) != -1,
retCurs = [];
//user and his friends with limited fields
retCurs.push( getUsersByIds( _.union(userId, userData.friends) ));
if(isFriend)
{
console.log('IS FRIEND');
this.added('users',userId, userData); //MERGE full fields if friend
//..add more fields and collections in reCurs..
}
return retCurs;
}
else
this.ready();
});
发布于 2014-03-22 17:32:14
请参阅文档中的粗体红色文本
如果在数组中返回多个游标,则它们当前都必须来自不同的集合。
有一个聪明-出版包,它增加了在发布时使用这个功能来管理同一个集合上的多个游标。这是相对较新的。
这或手动管理游标,在发布中使用‘that . and’、‘that. Either’和'this.changed‘。
https://stackoverflow.com/questions/22585893
复制相似问题