1

Each user has a gravatar or a profile image if uploaded. A list of rooms is shown (each has 2 parties). It shows the opposite party gravatar/uploaded image and his or her username.

Problem: The Gravatars do not appear but the uploaded images (if uploaded) appear. Gravatar does appear on the user's profile page (with params as the userId).

In console, Meteor.user() and Meteor.userId() returns ok. So is the md5hash which returns correct user details.

The user's profile:

profileImg 
  - uploaded 
username 
md5hash
emails

Each room contains

owner, receiver, people: [owner, receiver], id //roomId

allRooms.js

    Template.allRooms.onCreated(function () {
       this.autorun(() => {
          this.subscribe('rooms');
          this.subscribe('allUsers');
          $.cloudinary.config({ cloud_name: "mthh" }); 
       });
    });
    Template.allRooms.helpers({
       chatPerson(){ 
          return this.owner === Meteor.userId() ? Meteor.users.findOne(this.receiver) : Meteor.users.findOne(this.owner); 
       },
     });

allRooms.html

 {{#each rooms}}

     {{#with chatPerson}} <!-- doesnt work if {{#if chatPerson}} ..nothing shows -->
        {{#if profileImg.upload}} 
           <img src="{{c.url profileImg.upload format=format gravity='faces' mode='thumb' crop='thumb' width=60 height=60}}"> <!-- uploaded pic shows up --> 
        {{else}}
           <div class="avatar" style="background: url({{ avatar 60 chatPerson}}; height: 60px; width: 60px; float:left;"></div> <!-- if no uploaded pic, gravatar doesnt show up but console.log shows user object details and md5hash number --> 
        {{/if}}
     {{/with}}

     {{chatPerson.username}} roomId: {{_id}} <!-- shows up --> 
{{/each}}

Publication

Meteor.publish('allUsers', () => {   
    return Meteor.users.find({ 
        fields: { username: 1, emails: 1, profileImg: 1, md5hash: 1}
    })
});
Thinkerer
  • 1,606
  • 6
  • 23
  • 43

1 Answers1

1

Assuming you're using a helper to generate the avatar url such as:

Template.registerHelper( 'avatar',( avatarSize, user ) => {
  let md5hash = ( user && user.md5hash ) ? user.md5hash : this.md5hash;      
  md5hash = md5hash || "3eda6fcd3204ef285fa52176c28c4d3e"; // Equivalent to Gravatar.hash( 'none@none.com' );
  return Gravatar.imageUrl( md5hash, { secure: true, size: avatarSize, d: 'mm', rating: 'pg' } );  
});

Then you need to pass it a valid user object. In your code:

<div class="avatar" style="background: url({{ avatar 60 currentUser}}; height: 60px; width: 60px; float:left;"></div>

currentUser is not defined. Replace that with this which is the user context inside your {{#with}} block:

<div class="avatar" style="background: url({{ avatar 60 this}}; height: 60px; width: 60px; float:left;"></div>
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • `currentUser` is just an evocative variable name. Whatever you put there has to resolve to a `user` object that has an `md5hash` property. – Michel Floyd May 02 '17 at 03:25
  • `this` did not work, neither did `this.user` or `this.chatPerson`. hmm Let me know if you need some tests? I can post results here. – Thinkerer May 02 '17 at 15:49
  • Fixed a cut/paste error in my template helper code there... Try putting a breakpoint or doing `console.log(user)` in the `avatar` helper and make sure the helper is getting a user object passed to it and that the object has an `md5hash` key. – Michel Floyd May 02 '17 at 17:17
  • Let's jump to http://chat.stackoverflow.com/rooms/143310/meteor and continue there. – Michel Floyd May 03 '17 at 15:59