Meteor fusion cursors of the same collection

In my social application (like FB, for example), I have a strange need to combine two cursors of the same collection users in one edition!

The Meteor server will print this error: "The publish function returns multiple cursors for users in the collection."

Perhaps this is not possible to do in Meteor 0.7.2, maybe I'm wrong. But I saw that the cursor structure is pretty simple, since I can make a simple merge of the array and return the cursor back?

CUSTOMER

Meteor.subscribe('friendById', friend._id, function() {
    //here show my friend data and his friends
});

SERVER

//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();
});
+4
source share
2 answers

Decision

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();
});
+1
source

:

, .

smart-publish , . .

, , "this.added", "this.removed" "this.changed" .

+4

Source: https://habr.com/ru/post/1533022/


All Articles