1

I'm having the following data structure in my Meteor project:
- Users with a set of list-ids that belong to the user (author)
- Lists that actually contain all the data of the list and a set of user-ids that are allowed to view it (and a owner)

Now I'm trying to publish all Lists of a user to the client with the publish-with-relations-package (Toms version from GitHub). Here is a simple example:

Lists = new Meteor.Collection("lists");

if (Meteor.isClient) {

    Deps.autorun(function() {
        if (Meteor.userId()) {
            Meteor.subscribe("lists");
        }
    });

  Template.hello.greeting = function () {
    return "Test";
  };

  Template.hello.events({
    'click input' : function () {
      if (typeof console !== 'undefined')
        console.log(Lists.find());
    }
  });
}

if (Meteor.isServer) {

    Meteor.startup(function () {
        if ( Meteor.users.find().count() === 0 ) {
               var user = Accounts.createUser({        //create new user
                            username: 'test',
                            email: 'test@test.com',
                            password: 'test'
                        });

               //add list to Lists and id of the list to user
               var listid = new Meteor.Collection.ObjectID().valueOf();
               Meteor.users.update(user._id, {$addToSet : {lists : listid}});
               Lists.insert({_id : listid, data : 'content', owner : user._id});
        }
     });


Meteor.publish('lists', function(id) {

    Meteor.publishWithRelations({
        handle: this,
        collection: Lists,
        filter: _id,
        mappings: [{
            key: 'lists',
            collection: Meteor.users
        }]
    });
});

Meteor.publish("users", function(){
    return Meteor.users.find({_id : this.userId});
});


//at the moment everything is allowed
Lists.allow({
    insert : function(userID)
    {
        return true;
    },
    update : function(userID)
    {
        return true;
    },
    remove : function(userID)
    {
        return true;
    }
});

}

Publishing is not working and the Cursor doesn't contain the List element that is initiated.

Any idea how to fix this reactive join for publishing Lists of a certain user? Thanks in advance, any help is appreciated!

kerosene
  • 930
  • 14
  • 31

1 Answers1

4

Your test data isn't being inserted correctly. Have another look at my solution to your previous question. The key difference is that Accounts.createUser returns an id, not an object.

Anyway, PWR is a bit confusing but I think the correct approach is to publish the user, and then publish Lists as the relation. Even though each list document seems to have an owner, I'm assuming the goal is to publish all list documents in the user's lists array.

Meteor.publish('lists', function() {
  Meteor.publishWithRelations({
    handle: this,
    collection: Meteor.users,
    filter: this.userId,
    mappings: [{
      key: 'lists',
      collection: Lists
    }]
  });
});

Give that a try and let me know if you run into problems.

Community
  • 1
  • 1
David Weldon
  • 63,632
  • 11
  • 148
  • 146