Can't display profile data, subscription problem

Hello, I have a problem with displaying data I have inserted, the insertion is done correctly but for some reason it isn’t displaying, I have installed meteortoys to see if the data was inserted correctly but it always shows 0 and I have subscribe to my collection.

Here’s my publication

//this code was giving by another member of this forum, thanks again 
Meteor.publish('profiles', function () {
        const { userId } = this;

        if(userId){
            return Profiles.find({ userId });
        }
            
});

The subscription, nothing out of the ordinary

if (Meteor.isClient) {
    Meteor.subscribe('profiles');
}

And my insertion along with the helpers, I’ve created a profile collection for security reasons, I honestly don’t see any errors but I may be overlooking something, so thanks in advance and any advice is highly appreciated.

Template.Profile.events({
    "submit #profile-update": function (evt, err) {
        evt.preventDefault();
        var age = evt.target.age.value;
        var phone = evt.target.phone.value;
        var job = evt.target.job.value;
        var bday = evt.target.bday.value;
        var city = evt.target.city.value;
        var hi = evt.target.hi.value;
        var sn = evt.target.sn.value;

        if (Meteor.user && Profiles == null) {
            Profiles.insert({
                _id: Meteor.user()._id,
                Age: age,
                Phone: phone,
                Job: job,
                BirthDate: bday,
                City: city,
                HealthInsurance: hi,
                SecurityNumber: sn
            });
            Bert.alert('Edicion exitosa', 'success', 'growl-top-right');
        } else
        if (Profiles != null) {
            Profiles.update({
                Age: age,
                Phone: phone,
                Job: job,
                BirthDate: bday,
                City: city,
                HealthInsurance: hi,
                SecurityNumber: sn
            });

        } else {
            Bert.alert(err.reason, "danger", "growl-top-right");
        }
    },
    'change .PicInput': function (evt, template) {
        FS.Utility.eachFile(evt, function (file) {
            Images.insert(file, function (err, fileObj) {
                if (err) {
                    throw new error(err.reason, "danger", "growl-top-right")
                } else {
                    var userId = Meteor.user()._id;
                    var imageURL = {
                        "profile.image": "/cfs/files/images/" + fileObj._id
                    };
                    Meteor.users.update(userId, {
                        $set: imageURL
                    });
                }
            })
        });
    }
});

Template.Profile.helpers({
    profiles: function () {
        return Profiles.find({});

    },
    images: function () {
        if (Meteor.user) {
            return Images.find({});
        }
    }
});

There are some things that make it not work:

            Profiles.update({
                Age: age,
                Phone: phone,
                Job: job,
                BirthDate: bday,
                City: city,
                HealthInsurance: hi,
                SecurityNumber: sn
            });

This will fail, because you’re not ‘setting’ things. The first parameter is a selector, the second is the actual mutation. It should be like this:

Profiles.update({userId},  {
  $set: {
    userId: Meteor.userId(),
    Age: age,
    Phone: phone,
    Job: job,
    BirthDate: bday,
    City: city,
    HealthInsurance: hi,
    SecurityNumber: sn
  }
});

Notice the userId. This should be the userId of the currently logged in user. This also needs to happen on insert and this will fix your issue.

Now a bit about the security and about some style. (Just giving some tips that might save you one day) :slight_smile:

Use const and let instead of var
Use const wherever you can. Actually always. I myself almost never use let. Var is old and oyu should avoid it. https://dev.to/sarah_chima/var-let-and-const--whats-the-difference-69e

Use Meteor methods and check the logged in user

1 Like

Thank you so much for the help. :grinning:

1 Like