0

Can someone indicate whether the publish function needs monogdb's forEach operation, meteors' this.added or Meteor's observe function.

The current publish function works but not sure if this is reactive which is my main concern.

Will the forEach function operate for every new document added into the Blogs Collection?

user1 _id is embedded in several documents in a Blogs Collection like this:

creator: user1's_id

Within this Blog document there is also a field for one other user 'collaborator:

creator: user1's_id
collaborator: anotherUser_id

User1 may be the creator of hundred Blog documents but the collaborator will always be a different for every Blog document.

I would like to create the Meteor.publish function for the user who is the creator and return the following two cursors

  1. all the Blog documents for which user1 is the creator
  2. all the Meteor.users() who are the collaborator

    Meteor.publish('creatorBlogContext', function() {
    
        var user = Meteor.users.findOne(this.userId)
    
        if (user && user.profile.userType === 'creator') {
            var blogsCursor =  Blogs.find({'creator': user._id}); //returns all blog docs the user has created.
    
            var userIdsArray = []
    
            Blogs.find({'creator': user._id}).forEach(function(doc) {
                userIdsArray.push(doc.collaborator)
            })
    
            var usersCursor = Meteor.users.find({_id: {$in: userIdsArray}})
    
            return [blogsCursor, usersCursor];
        }
    });
    

    Will the above function reactively re-run when a new Blog document so the forEach function pushes out the Meteor.user of the collaborator?

Note: I don't wish to user iron router and do a findOne in the publish function depending on which blog the creator is viewing. I wish to do this the core meteor way, so no packages suggestions please.

Thank you and please ask for further information if I have not been clear.

meteorBuzz
  • 3,110
  • 5
  • 33
  • 60
  • 1
    Details for reactive joins (including non-package solutions) can be found in the answer to [this question](http://stackoverflow.com/questions/26398952/meteor-publish-publish-collection-which-depends-on-other-collection). – David Weldon Apr 24 '15 at 13:45
  • I know how to do reactive joining. My question is regarding sending the cursor of Meteor.users.find() for every userId of the 'collaborator' field of every Blog document the creator owns. – meteorBuzz Apr 24 '15 at 14:04

0 Answers0