Problem displaying profile data of the user

#1

Hello, I know I already asked something about this already but after reading my question I wasn’t really clear about a few things.
I have published all of my users because there is a section where I need to look for them all but I don’t know if this is the right way, also I have published all of the users alongside the profiles, I’ve created a separate collection for security reasons.

Meteor.publish('profiles', function() {
    return Meteor.users.find() || Profiles.findOne();
});

When I delete Meteor.users.find() this error appears

Exception from sub profiles id BdHAnsFJWWYXBFSXX TypeError: Profiles.findOne(...).fetch is not a function
I20190103-23:55:19.411(-4)?     at Subscription.<anonymous> (server/Publish.js:12:32)
I20190103-23:55:19.413(-4)?     at packages/matb33_collection-hooks.js:307:21
I20190103-23:55:19.414(-4)?     at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1304:12)
I20190103-23:55:19.414(-4)?     at Subscription._handler (packages/matb33_collection-hooks.js:306:28)
I20190103-23:55:19.415(-4)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190103-23:55:19.416(-4)?     at DDP._CurrentPublicationInvocation.withValue (packages/ddp-server/livedata_server.js:1043:15)
I20190103-23:55:19.417(-4)?     at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1304:12)
I20190103-23:55:19.418(-4)?     at Subscription._runHandler (packages/ddp-server/livedata_server.js:1041:51)
I20190103-23:55:19.419(-4)?     at Session._startSubscription (packages/ddp-server/livedata_server.js:859:9)
I20190103-23:55:19.419(-4)?     at Session.sub (packages/ddp-server/livedata_server.js:625:12)
I20190103-23:55:19.421(-4)?     at packages/ddp-server/livedata_server.js:559:43

After adding the Meteor.users.find() again it disappears

And when I insert data in my inputs to fill the collection and display then, the subscription works for a second showing me with the meteortoys that indeed it was inserted but it disappears and I have no clue as to why of this, when I check minimongo the data is there.

Here’s my insert method:

   if (Meteor.user) {
            Profiles.insert({
                _id: Meteor.user()._id,
                Age: evt.target.age.value,
                Phone: evt.target.phone.value,
                Job: evt.target.job.value,
                BirthDate: evt.target.bday.value,
                City: evt.target.city.value,
                HealthInsurance: evt.target.hi.value,
                SecurityNumber: evt.target.sn.value,
                Rnc: evt.target.rnc.value,
                Exequatur: evt.target.exe.value
            });
            Bert.alert("Insercion exitosa", "success", "growl-top-right");
        }

Any help is really appreciated.

#2

What you have here is conditional logic that says compute get the return value from the function Meteor.users.find and if it is a non falsey value, return it, otherwise get the return value of the function Profiles.findOne and return that value.

I think what you are trying to do however is to publish user records along with the corresponding profile record. For that you’ll need a package such as reywood:publish-composite.

#3

Oh thanks on that part I definetly wasn’t looking that publication that way but there’s still the problem with displaying the inserted data to which I have no clue on how to solve it ^^!

#4

The problem is that the profile data isn’t published to the client and so when you insert, you see a the record temporarily inserted (latency compensation) into the client side cache (Minimongo). Once the insert completes though and the client hasn’t received a message from the server telling the client that the record should be part of the client side cache, that record gets invalidated and disappears.

A quick and dirty fix until you can get the publish-composite package figured out would be to return a cursor from both the users collection and the profiles collection as an array which will publish all records from both collections… This isn’t ideal, but neither is not limiting the amount of users you publish :slight_smile:

Meteor.publish('profiles', function() {
    return [Meteor.users.find(), Profiles.find()];
});

Also you might find socialize:user-profile interesting, as well as any number of socialize packages.

2 Likes
#5

I see, thanks for the help I really appreciate it :grinning:

#6

You might be interested to know, however, that creating a new collection solely due to security is not necessary as you have full control over what gets published and what doesn’t.

1 Like