[Solved] Publish data from a different collection for each user

Hi, I’m trying to make a list of similar records for allApp, when I try to publish the data so that they are only displayed for a specific user, I only get them to keep showing the same data to all registered users. I only wish that if that user keeps that data to them Show only for him.

I leave the code to see if anyone helps me with the cause of the problem.

/// client 
AddPerson = new Mongo.Collection("addperson");

Template.AddPersonas.onCreated(function(){
   this.autorun(() => {
     this.subscribe('people');
   });
});

Template.AddPersonas.events({
  'submit #formPersona' (event, template) {
      event.preventDefault();
      const target = event.target;
      var name = target.name.value;
      var lastname= target.lastname.value;
      var phone= target.phone.value;
      var selectPerson= target.selectPerson.value;
      // var hora= target.time.value;
      // var Recordatorio= target.Recordatorio.value;
      if (selectPerson==="Otro"){
      selectPerson= target.otro.value;
    } else{
      selectPerson=selectPerson;
    }

    let objPerson= {
      name: name,
      lastname:lastname,
      phone:phone,
      selectPerson:selectPerson
    }
      Meteor.call('addTodo', objPerson);
},

});

/// server 
Meteor.methods({
  addTodo: function(objPerson){
    AddPerson.insert( {
        name: objPerson.name,
        lastname: objPerson.lastname,
        phone:objPerson.phone,
        selectPerson:objPerson.selectPerson,
        createdAt: new Date(),
        userId: Meteor.userId(),
    });
  },
});

/// publish.js 
Meteor.publish('people', function(){
   return AddPerson.find({});
  //  return AddPerson.find({_id: this.userId});
});

Try this for your publication:

Meteor.publish('people', function() {
  return AddPerson.find({ userId: this.userId });
});

Hi thanks if I had tried that way but I did not work, I managed to make it work in the helpers as follows

Template.persona.helpers({
addperson: function() {
    var id= Meteor.userId();
    return AddPerson.find({userId: id});
}
});

Yeah, but that’s not good, as you will still be publishing (sending) all of the data to each client, which is a huge security risk. You need to filter that data at the publication level, not the helper level. I could go into your app and see everyone’s data if you are returning all data from the publication.

Ok thanks for the data I already put it and I work.

I can’t say for certain, but this sounds like autopublish is still installed.

Remove this.autorun from onCreated because you don’t have anything on your autorun you have not attached any reactive variable to fire autorun it’s better to remove autorun.Directly use Meteor.subscribe() Check for autopublish package if it’s availabe in your project remove it.

1 Like

thanks, @shrinath If that too I did along with what I recommend @vigorwebsolutions is already working

Okay Cool. Use dynamic subscription.